Friday, 20 April 2018

//wap to check string is anagram or not?

//write a program to check string are anagram or not?
package govindtest;
import java.util.Arrays;
public class AnagramCheck {
public static void checkAnagram(String s1,String s2)
{
String s3=s1.replaceAll("\\s", "");
String s4=s2.replaceAll("\\s", "");
int x=s3.length();
int y=s4.length();
if(x==y)
{
char[] ch1 = s3.toLowerCase().toCharArray();
char[] ch2 = s4.toLowerCase().toCharArray();
Arrays.sort(ch1);
Arrays.sort(ch2);
if(Arrays.equals(ch1, ch2))
System.out.println("String are anagram");
}
else
{
System.out.println("String are not Anagram");
}
}
public static void main(String[] args) {
AnagramCheck.checkAnagram("keep","pee k");
AnagramCheck.checkAnagram("the cla ss room", "school master");
AnagramCheck.checkAnagram("maa", "AAm");
AnagramCheck.checkAnagram("integral", "TRIANGLE");
AnagramCheck.checkAnagram("silent", "listen");
AnagramCheck.checkAnagram("DORMITORY", "Dirty Room");
AnagramCheck.checkAnagram("ASTRONOMERS", "NO MORE STARS");
AnagramCheck.checkAnagram("Mother In Law", "Hitler Woman");
AnagramCheck.checkAnagram("Debit Card", "Bad Credit");
AnagramCheck.checkAnagram("SiLeNt CAT", "LisTen AcT");
AnagramCheck.checkAnagram("govindkhan", "ballabhkhan");
}
}
o/p:-
String are anagram
String are anagram
String are anagram
String are anagram
String are anagram
String are anagram
String are anagram
String are anagram
String are anagram
String are anagram
String are not Anagram

No comments:

Post a Comment