Thread: In need of help with anagrams

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102

    In need of help with anagrams

    I am trying to write a program with a function that takes two char arrays, array1, array2. The function should compare array2 with array1, if array2 is an anagram of array1, then it should indicate so. Here is my idea:

    I am writing a bool function and I have two loops in the function. First, just pick one character from array1, loop it through array2, and whenever there is an encounter, increase count by 1.

    Problem is, this will count through every character, including repeatedly counting every character that appears again. I need help in counting the number of occurrences of each character only once, and ignoring any repeated one. That way, I can easily compare the two strings, and return false if the count is not equal to the length of string1. And here is the function:

    Code:
    bool IsAnagram(char *array1, char *array2) {
    
       bool anagram =  true;
    
       int counter = 0;
    
         for (int i = 0; array1[i]; ++i)  {                 
             
             for (int k = 0; array2[k]; ++k) 
                               
                 if(array1[i] == array2[k]) 
             
                 counter++;
    
            //if counter is not equal to length of array1, return  anagram = false;
             
            }
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Looks like you'll need some auxiliary arrays of counts for each letter then.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    You can't ignore repeated charaters in the arrays. The arrays need to be the same length and have the exact same characters.

    Of course to really be an anagram both arrays need to be an actual word or phrase.
    Last edited by DaveH; 05-20-2011 at 10:39 AM.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    OK, maybe I need to explain it better. I know you can't ignore repeated characters as for the condition of an anagram to be fulfilled, every character has to appear in array1 as much as it appears in array2. However this is what I meant. Say arary1 is : desperation and array2 = aropeendsit.

    Here, what my loop did was, it takes d, counts its number of occurrences in array2, then does the same for e and so on. Now because there is more than one e, it will count the occurrences of es two times instead of counting their occurrence only once. But I think I figured out how to solve it, with a break.
    Code:
    bool IsAnagram(char *array1, char *array2) {
    
       bool anagram =  true;
    
       int counter = 0;
    
         for (int i = 0; array1[i]; ++i)  {
    
             counter1 = 0;                    
             
             for (int k = 0; array2[k]; ++k) 
                               
                 if(array1[i] == array2[k]) {
                     
                     counter1++;                                                                   
                                 
                                 if(counter1 >1) break;
    
                                 
                                 
                                 counter++; 
    
                                 }  
             
                 counter++;
    
            //if counter is not equal to length of array1, return  anagram = false;
             
            }
    
    }
    Last edited by Dontgiveup; 05-20-2011 at 11:06 AM.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    I still think your method is flawed. You HAVE to keep track of the number of occurences of each letter in the arrays. First check if the arrays are the same length. If not, they can't be anagrams. Then count the occurences of each letter in array1. Do the same for array2. Check that the letters and the number of occurences in each array are exactly the same.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Now I just need help in changing array1 and arary2 to lower-case in order to overcome problems that might arise as a result of case sensitivity.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Quote Originally Posted by DaveH View Post
    I still think your method is flawed. You HAVE to keep track of the number of occurrences of each letter in the arrays. First check if the arrays are the same length. If not, they can't be anagrams. Then count the occurrences of each letter in array1. Do the same for array2. Check that the letters and the number of occurrences in each array are exactly the same.
    Yeah I did count the length of the arrays first and compared them, and if they are not equal, then they can't be anagram. The code is a bit long, so I only posted the part where there was any relevance and where I had questions (but I would be happy to post all of it if you want to see it all).

    I think my method isn't flawed (all the tests have done so far have turned out to be successful though you can never be 100% certain of whether a program is 100% correct always). What I did with the break loop was it counts each character once as they appear, so in the end all characters are counted, and because we are only counting characters that appear in both arrays, we can compare the final count with the length of array1, and if the length is not equal, then the function return false. As I said, can't be 100% certain but the testing so far has been 100% right.
    Last edited by Dontgiveup; 05-20-2011 at 11:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. anagrams
    By nospammax in forum C Programming
    Replies: 11
    Last Post: 04-26-2011, 12:16 PM
  2. It's your turn (job anagrams)
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-18-2007, 12:18 PM
  3. anagrams
    By InvariantLoop in forum C Programming
    Replies: 12
    Last Post: 03-10-2005, 05:22 PM
  4. Anagrams
    By misswaleleia in forum C Programming
    Replies: 2
    Last Post: 05-31-2003, 06:37 PM
  5. Anagrams
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 03-08-2002, 04:40 PM