
ANAGRAM program:
eg: "parliment"-"partial men" "software"-"swear oft"
write a java program that figures out whether one string in an anagram of another string. Program should ignore whitespace and punctuation.

import java.util.*;
public class AnagramExample {
public static boolean checkAnagrams(String s1, String s2){
String st1 = remove(s1);
String st2 = remove(s2);
st1 = st1.toLowerCase();
st2 = st2.toLowerCase();
st1 = sort(st1);
st2 = sort(st2);
return st1.equals(st2);
}
public static String remove(String st) {
int i, len = st.length();
StringBuilder builder = new StringBuilder(len);
char ch;
for (i = (len - 1); i >= 0; i--) {
ch = st.charAt(i);
if (Character.isLetter(ch)) {
builder.append(ch);
}
}
return builder.toString();
}
public static String sort(String st) {
char arr[] = st.toCharArray();
Arrays.sort(arr);
return new String(arr);
}
public static void main(String[] args) {
String string1 = "Parliament";
String string2 = "partial men";
System.out.println(" String 1: " + string1);
System.out.println(" String 2: " + string2);
System.out.println();
if (checkAnagrams(string1, string2)) {
System.out.println("Strings are anagrams!");
} else {
System.out.println("Strings are not anagrams!");
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.