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

Dani AI

Generated

The immediate cause is that the program only updates the letter frequency array for alphabetic characters, so two inputs that contain no letters (blank lines, only digits or only punctuation) leave every frequency at zero and the code reports "anagrams". spotted this, and the robust fix is to validate after normalizing the inputs, not just by checking the raw buffer lengths.

Recommended validation steps to add (apply these before declaring anagrams):

  1. Normalize each fgets buffer: remove the trailing newline, then build a filtered string that contains only alphabetic characters converted to a common case. Track the filtered length for each input.
  2. If either filtered length is zero, reject the input as invalid (this handles "enter" + "enter" and "33467" + "22113").
  3. If the filtered lengths differ, they cannot be anagrams.
  4. If lengths are equal and > 0, proceed with the frequency-count or sorting comparison you already have.

Safety and edge notes: always check fgets returned value for NULL; cast to (unsigned char) when passing characters to isalpha/toupper to avoid undefined behavior on platforms where char is signed; decide up front whether digits/punctuation should be treated as errors or stripped (phrases like "dirty room" vs "dormitory" require stripping spaces). For locale/Unicode input, isalpha will only cover the C locale or the currently set locale; wide-character handling (wchar_t) is needed for full Unicode support.

Useful test cases to add to the suite: blank vs blank (invalid), numeric-only vs numeric-only (invalid), "listen" vs "silent" (anagram), "dirty room" vs "dormitory" (anagram if spaces/punctuation are stripped), and mixed-case inputs. Applying the normalized-length check above will eliminate the false positives seen by .

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.