Write a Java Program to Check Strings Anagram or not to each other
import java.util.Scanner;
public class StringAnagram
{
public static void main(String[] args)
{
String s1,s2; //for input strings
int length, length1, length2;//for string lengths
int i, j; //for 2 iteration
int found=0, not_found=0; //to check string anagram of not
Scanner scan = new Scanner(System.in);
//taking string input from user
System.out.println("Enter first String :- ");
s1=scan.nextLine();//scanning first string
System.out.println("Enter Second String :- ");
s2=scan.nextLine();//scanning second string
length1=s1.length();
length2=s2.length();
//checking condition both strings have same length or not
if(length1==length2)
{
length=length1;
for(i=0;i<length;i++)
{
found=0;
for(j=0;j<length;j++)
{
//condition to check strings are anagram or not
if(s1.charAt(i)==s2.charAt(j))
{
found=1;//if same then found=1
break;
}
}
//if not same then not_found=1
if(found==0)
{
not_found=1;//means both strings are not anagram
break;
}
}
if(not_found==1)
{
System.out.println("strings are not anagram to each other");
}
else
{
System.out.println("strings are anagram to each other");
}
}
else
{
System.out.println("please enter same length strings to be an anagram");
}
}
}
Output: Enter a Number: 159874632 Given Number:-159874632 After Reverse :-236478951











