Results 1 to 4 of 4
Thread: Anagram Tester
- 09-26-2013, 11:56 PM #1
Senior Member
- Join Date
- Aug 2013
- Location
- Sweden
- Posts
- 163
- Rep Power
- 8
Anagram Tester
Hello!
This program down below lets you enter two strings, then compares them and outputs if they are anagrams of each other or not. However, I think that it's a little bit too long and wonder if it can be done in an easier and/or shorter way.
(And no, this is not my homework or anything like that, I'm just doing this all alone of sheer curiosity because I want to learn Java, believe me or not.)
Thanks in advance! :)
Java Code:import java.util.*; import javax.swing.*; public class tjena{ public static void main(String[] args){ String t1 = JOptionPane.showInputDialog("Enter a string of text"), s1; s1 = t1.toUpperCase(); s1 = s1.replaceAll("\\s", ""); String t2 = JOptionPane.showInputDialog("Enter another string of text"), s2; s2 = t2.toUpperCase(); s2 = s2.replaceAll("\\s", ""); boolean anagram = anagramTest(s1, s2); t1 = anagram ? "Your strings are anagrams of each other!" : "Your strings are not anagrams of each other!"; JOptionPane.showMessageDialog(null, t1); } static boolean anagramTest(String s1, String s2){ boolean anagram = false; if(s1.length() == s2.length()){ int firstAnagram[] = alphabetTester(s1), secondAnagram[] = alphabetTester(s2); for(int x=0;x<firstAnagram.length;x++){ if(firstAnagram[x] != secondAnagram[x]) break; else if(firstAnagram[x] == secondAnagram[x] && x == firstAnagram.length - 1) anagram = true; } } return anagram; } static int[] alphabetTester(String s){ // THIS PART RETURNS AN ARRAY WITH THE LENGTH OF THE ALPHABET! THE CELLS CONTAIN int test[] = new int[25]; // THE AMOUNT OF A:S, B:S, C:S, ETC. THAT THE STRING CONTAINED IN ALPHABETICAL ORDER for(int x=65, z = 0;x<=90;x++,z++){ for(int y=0;y<s.length();y++){ if(s.charAt(y) == (char)x) test[z]++; } } return test; } }
- 09-27-2013, 01:54 AM #2
Re: Anagram Tester
One trick for counting characters whose values are contiguous is to use the values of the characters to compute the index. If 'a' is the first char and 'z' is the last, then the index values would be: 'a'=0, 'b'=1, 'c'=2 etc
Compute these indexes by: theChar - 'a' where theChar is a char in the range 'a' to 'z'
BTW Hardcoding 65 and 90 makes the code hard to understand. It is better to code 'A' and 'Z'Last edited by Norm; 09-27-2013 at 01:59 AM.
If you don't understand my response, don't ignore it, ask a question.
- 09-27-2013, 05:28 AM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Anagram Tester
Yes, there is a much simpler way. I came up with this many years ago but I'm certain someone else has done it too. All anagrams of each other sort to the same string. Just convert the strings into separate character arrays and sort each array. Then just iterate thru the arrays comparing the characters in each position. If they are all the same, the two strings are anagrams of each other. This can be done after the length checks.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-27-2013, 10:09 AM #4
Senior Member
- Join Date
- Aug 2013
- Location
- Sweden
- Posts
- 163
- Rep Power
- 8
Re: Anagram Tester
Is this what you meant Jim?
Java Code:import java.util.*; public class tjena{ public static void main(String[] args){ System.out.print("Enter a string of words: "); char word1[] = transformingString(); System.out.print("Enter another string of words: "); char word2[] = transformingString(); word1 = sorter(word1); word2 = sorter(word2); String s = Arrays.equals(word1, word2) ? "Your strings are anagrams of each other!" : "Your strings are not anagrams of each other!"; System.out.print(s); } static char[] sorter(char word[]){ for(int x=0, z=0;x<=25;x++) for(int y=0;y<word.length;y++) if((char)(x+65) == word[y]){ char c = word[y]; word[y] = word[z]; word[z] = c; z++; } return word; } static char[] transformingString(){ Scanner hej = new Scanner(System.in); String s = hej.nextLine(); s = s.replaceAll("\\s+", ""); String s1 = s.toUpperCase(); char test[] = s1.toCharArray(); return test; } }
Similar Threads
-
Anagram solver help using recursive backtracking
By Schooling in forum New To JavaReplies: 2Last Post: 05-24-2012, 04:47 AM -
Anagram game - method heko
By VaBhoy in forum New To JavaReplies: 1Last Post: 11-28-2009, 02:25 PM -
Help! Anagram Games
By geeky123 in forum New To JavaReplies: 6Last Post: 10-13-2008, 05:16 PM -
Recursive Anagram
By zoe in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 07:15 AM
Bookmarks