i am trying to create a program for string anagram that follows these conditions:
- method should allow only letters, white space, commas and dots in an anagram. If there are any other characters, then the string cannot contain an anagram.
- The method should ignore all white space, commas and dots when it checks the text.
- If there are no letters in the text, then the text cannot be an anagram.
import java.util.Arrays; import java.util.Scanner; public class StringAnagram { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter first string: "); String first = in.nextLine().toUpperCase(); System.out.print("Enter second string: "); String second = in.nextLine().toUpperCase(); String result = isAnagram(first, second); System.out.println(result); } private static String isAnagram(String first, String second) { String answer = ""; if (first.matches("[A-Z\.\,\s]")) { String st = first.replaceAll("\.\,\s", ""); String nd = second.replaceAll("\.\,\s", ""); char[] arraySt = st.toCharArray(); char[] arrayNd = nd.toCharArray(); Arrays.sort(arraySt); Arrays.sort(arrayNd); if(Arrays.equals(arraySt, arrayNd)) { answer = "Anagram."; } else { answer = "No anagram."; } } else { answer = "No anagram."; } return answer; } }
However when the program tests these 2 sentences, they are not anagram but they should be anagram. I have no idea where to look for mistake.
- Eleven plus two is thirteen.
- Twelve plus one is thirteen.
Advertisement
Answer
If you start your method as follows, it will fulfil validations mentioned in the 1st
and the 3rd
points of your question:
if (first == null || second == null || first.equals("") || second.equals("") || !first.matches("[A-Za-z,. ]+") || !second.matches("[A-Za-z,. ]+")) { return "No anagram."; }
The next thing you should do is to replace all white space, commas and dots with ""
in order to ignore them:
String st = first.replaceAll("[,. ]+", ""); String nd = second.replaceAll("[,. ]+", "");
The complete code is as follows:
private static String isAnagram(String first, String second) { if (first == null || second == null || first.equals("") || second.equals("") || !first.matches("[A-Za-z,. ]+") || !second.matches("[A-Za-z,. ]+")) { return "No anagram."; } String answer = ""; String st = first.replaceAll("[,. ]+", ""); String nd = second.replaceAll("[,. ]+", ""); if (st.equals("") || nd.equals("")) { return "No anagram."; } char[] arraySt = st.toCharArray(); char[] arrayNd = nd.toCharArray(); Arrays.sort(arraySt); Arrays.sort(arrayNd); if (Arrays.equals(arraySt, arrayNd)) { answer = "Anagram."; } else { answer = "No anagram."; } return answer; }
A test run:
Enter first string: london Enter second string: britain No anagram.
Another test run:
Enter first string: ram Enter second string: mar Anagram.
Another test run:
Enter first string: . Enter second string: . No anagram.
Another test run:
Enter first string: , Enter second string: . No anagram.
Another test run:
Enter first string: ra.m Enter second string: a.m.r Anagram.