Results 1 to 4 of 4

Thread: Anagram Tester

  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    157
    Rep Power
    2

    Smile 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;
    	}
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,454
    Rep Power
    25

    Default 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 12:59 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Anagram Tester

    Quote Originally Posted by Zelaine View Post
    Hello! ...wonder if it can be done in an easier and/or shorter way.
    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,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    157
    Rep Power
    2

    Default 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

  1. Anagram solver help using recursive backtracking
    By Schooling in forum New To Java
    Replies: 2
    Last Post: 05-24-2012, 03:47 AM
  2. Anagram game - method heko
    By VaBhoy in forum New To Java
    Replies: 1
    Last Post: 11-28-2009, 01:25 PM
  3. Help! Anagram Games
    By geeky123 in forum New To Java
    Replies: 6
    Last Post: 10-13-2008, 04:16 PM
  4. Recursive Anagram
    By zoe in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •