JavaNo Comments

default thumbnail

Two String is anagram which have same kind of characters but different order or way . Like Example : “Race”-“arce”,”Apple”-“pplae” . Here is how to check String is Anagram or not Anagram using java.

In Using Array Sort method :-Arraysort(character Array )

import java.util.*;
class gram
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
//word print..
System.out.println(” Enter First Word “);
//word stored in String.
String str=sc.nextLine();
//string change in chatacter Array..
char ch[]=str.toCharArray();
//sort the ch array
Arrays.sort(ch);
System.out.println(” Enter Second Word “);
String str1=sc.nextLine();
char ch1[]=str1.toCharArray();
Arrays.sort(ch1);
for(int i=0;i<str.length();i++)
{
for(int j=0;j<str1.length();j++)
{
}
}
if(Arrays.equals(ch,ch1))
{
System.out.println(” This is Anagram ” );
}
else
{
System.out.println(” This is not Anagram “);
}
}

}

Be the first to post a comment.

Add a comment