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