I'm trying to improve the validity checks in my program, but no matter what I do, I can't seem to come up with one that catches the following:

1) If you input "enter" "enter" the program reads it as a valid anagram.
2) if you input all number "33467" "22113" the program also reads it as a valid anagram.

Any suggestions?

/*
  Program assignment name: Anagrams

  Author:Christopher Deaver
*/

#include<stdio.h>
#include<string.h>
#include<ctype.h>

int main()
{
   char word1[50],word2[50];
   int count[26]={0};
   int i;
	
   fputs("Enter the 1st word: ", stdout);
   fflush(stdout); 
   fgets(word1, sizeof word1, stdin);
	
   fputs("Enter the 2nd word: ", stdout);
   fflush(stdout); 
   fgets(word2, sizeof word2, stdin);
			
   for(i=0; i<50; i++)
   {    
      if(word1[i] == '\0')
      {  
	 break;
      }
      else if(isalpha(word1[i]))
      { 
	 word1[i]=toupper(word1[i]);
	 count[word1[i]-65]++;
      }
   }
	
   for(i=0; i<50; i++)
   {
      if(word2[i] == '\0')
      {
	 break;
      }
      else if(isalpha(word2[i]))
      { 
	 word2[i]=toupper(word2[i]);
	 count[word2[i]-65]--;
      }
   }
	
   for(i=0; i<26; i++)
   {
      if(count[i] != 0)
      {
	 printf("The words are not anagrams\n");
	 return 0;
      }
   }
	
   printf("The words are anagrams \n");     
   return 0;
}

Recommended Answers

All 3 Replies

This will get you started.
You still have to fix if you enter a space for word1 and word2 to will say they are anagram.
Because the check on count is against zero, if we never increment or decrement count it will still be zero and it thinks its an anagram.

/*
  Program assignment name: Anagrams

  Author:Christopher Deaver
*/

#include<stdio.h>
#include<string.h>
#include<ctype.h>

int main()
{
   char word1[50],word2[50];
   int  count[26]={0};
   int  i,yesTheyAre=0;
	
   fputs("Enter the 1st word: ", stdout);
   fflush(stdout); 
   fgets(word1, sizeof word1, stdin);
	
   fputs("Enter the 2nd word: ", stdout);
   fflush(stdout); 
   fgets(word2, sizeof word2, stdin);

   /* 
      If they are of different length then can't be anagrams 
      Also, the fgets stores the '\n' so that is why the 
      lenght has to be larger than 1
   */
   if (strlen(word1) == strlen(word2) && strlen(word1) > 1) {
      for(i=0; i<50; i++)
      {    
         if(word1[i] == '\0')
         {  
	    break;
         }
         else if(isalpha(word1[i]))
         { 
	    word1[i]=toupper(word1[i]);
	    count[word1[i]-65]++;
         }
      }
	   
      for(i=0; i<50; i++)
      {
         if(word2[i] == '\0')
         {
	    break;
         }
         else if(isalpha(word2[i]))
         { 
	    word2[i]=toupper(word2[i]);
	    count[word2[i]-65]--;
         }
      }
      for(i=0; i<26; i++)
      {
         if(count[i] == 0)
         {
            yesTheyAre++;
         }
      }
   }	   

   if ( yesTheyAre == 26 ) {	
     printf("The words are anagrams \n");     
   }
   else {
     printf("The words are not anagrams\n");
   }
   return 0;
}

I forgot, I didn't fix the input of "12" and "13. That prints out that they are anagrams for the same reason that space and space does.

I forgot, I didn't fix the input of "12" and "13. That prints out that they are anagrams for the same reason that space and space does.

I understand. Thank you for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.