Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,443 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,226 people online right now. Registration is fast and FREE... Join Now!




Anagram Checker

 
Reply to this topicStart new topic

Anagram Checker, Error trying to check for anagram

alberts613
21 Nov, 2007 - 03:27 PM
Post #1

New D.I.C Head
*

Joined: 21 Nov, 2007
Posts: 3


My Contributions
I've tried to write a program that inputs 2 phrases and checks if they are anagrams. I'm trying to do this by checking the amount of each letter in each phrase and then putting the amount of each letter in a different array. For example if there are 3 a's in the first phrase, alphCount[0] would = 3. Any help would be grateful. Code is below:

CODE

int main(int argc, char *argv[])
{
    char phrase1[500], phrase2[500];
    cout << "Enter the first string" << endl;
    cin.getline (phrase1, 500);
    cout << "Enter the second string" << endl;
    cin.getline (phrase2, 500);
    
  char LowAlphList[] = {"abcdefghijklmnopqrstuvwxyz"};
  char UpAlphList[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
  
  int alphCount1[26];
  int alphCount2[26];
  
  int count = 0;
  int count2 = 1;
  for(int i =0; i<26; i++){
          for(int j =0; j<500; j++){
                  if((phrase1[j] == LowAlphList[i]) || (phrase1[j] == UpAlphList[i])){
                            alphCount1[count] =  count2;
                           count2++;
                  }
          }
          count2=0;
          count++;

  }

  int Count = 0;
  int Count2 = 1;
  for(int i =0; i<26; i++){
          for(int j =0; j<500; j++){
                  if((phrase2[j] == LowAlphList[i]) || (phrase2[j] == UpAlphList[i])){
                           alphCount2[Count] =  Count2;
                           Count2++;
                  }
          }
          Count2=0;
          Count++;

cout << alphCount1 << endl << alphCount2 << endl;

  }                            
  int counter =0;
  for(int i=0; i <26; i++){
      if(alphCount1[i]=alphCount2[i]){
         counter=counter;
      }
      else{
         counter++;
      }

  if(counter == 0){
     cout << "Phrases are anagrams" << endl;
  }
  else{
     cout << "Phrases are not anagrams" << endl;
  }
            
    
  system("PAUSE");
  return EXIT_SUCCESS;
}

*edit: please use code tags next time. code.gif

This post has been edited by Martyr2: 21 Nov, 2007 - 04:03 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Anagram Checker
21 Nov, 2007 - 04:58 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,654



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Hi albert,

I think you are going to like this solution for you. Based on your code I set up a simultaneous test on both strings to determine if they are anagram. Thus we cut down your loop by magnitudes enhancing performance and accuracy. Check it out...

CODE

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    char phrase1[500], phrase2[500];
    cout << "Enter the first string" << endl;
    cin.getline (phrase1, 500);
    cout << "Enter the second string" << endl;
    cin.getline (phrase2, 500);
    
    char LowAlphList[] = {"abcdefghijklmnopqrstuvwxyz"};
    char UpAlphList[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};

    // Set our arrays to zero all the way through
    int alphCount1[26] = {0};
    int alphCount2[26] = {0};

    // Get the length of each phrase
    int count = (int)strlen(phrase1);
    int count2 = (int)strlen(phrase2);

    // If they are not identical length, we automatically know they are not anagrams.
    if (count == count2) {

        // Loop through the letters of each phrase (which are identical)
        for (int i = 0; i < count; i++) {

            // Loop through the alphabet for both phrases simulatneously
            // Increment their respective spots in their own array
            // They are in sync, like it?;)
            for (int j = 0; j < 26; j++) {
                if ((phrase1[i] == LowAlphList[j]) || (phrase1[i] == UpAlphList[j])) {
                    alphCount1[j] += 1;
                }

                if ((phrase2[i] == LowAlphList[j]) || (phrase2[i] == UpAlphList[j])) {
                    alphCount2[j] += 1;
                }
            }
        }

        bool identical = true;

        // Last, compare the two arrays to see if they have the same counts.
        for (int i = 0; i < 26; i++) {
            if (alphCount1[i] != alphCount2[i]) {
                identical = false;
            }
        }

        // If the arrays have the same count, identical flag would not be set to false.
        if (identical) { cout << "They are anagrams" << endl; }
        else { cout << "They are not anagrams" << endl; }
    }
    else { cout << "They are not anagrams" << endl; }

    system("PAUSE");
    return 0;
}


You can read through the in code comments to see what I am doing. However, it is pretty simplistic in nature. Once we determine they are the same size, we can test both strings using the same looping mechanism. We then just compare the results at the end.

Give it a whirl and enjoy!

"At DIC we be dual wielding, string checking, anagram testing code ninjas!" decap.gif

This post has been edited by Martyr2: 21 Nov, 2007 - 05:01 PM
User is online!Profile CardPM
+Quote Post

alberts613
RE: Anagram Checker
21 Nov, 2007 - 05:20 PM
Post #3

New D.I.C Head
*

Joined: 21 Nov, 2007
Posts: 3


My Contributions
Martyr2 - I appreciate the quick response. Your editing seems to work, but not all the time. I've tried a couple anagrams and it works for a couple, however, not all of them. For example, try Woman Hitler and Mother in Law.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Anagram Checker
21 Nov, 2007 - 05:32 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
The function first checks whether the strings are the same length, so given that there is one more space in "mother in law" than in "woman hitler", that test will immediately fail.

If you don't need to preserve the original string, I'd first go through and remove any spaces (and perhaps punctuation) from the strings, and then use the code Martyr2 worked up.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 12:38PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month