So the project is to write a program (for a beginners C class) that compares 2 words and see if there anagrams of each other. I'm about halfway through the program and it was working fine until i tried to add the IF STATEMENTS with isalpha. What I'm trying to do is say:
If the string entered is all letters then continue with the program. If not terminate.
What am I doing wrong and are there any other obvious mistakes in my code?
Thanks
/*
Program assignment name: Anagrams
Author:Christopher Deaver
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char word1[100], word2[100];
int count[26];
int i, j;
fputs("Enter the 1st word: ", stdout);
fflush(stdout);
fgets(word1, sizeof word1, stdin);
for (i=0; i<100; i++)
{
if(isalpha(word1[i]))
{
word1[i] = toupper(word1[i]);
}
else;
{
printf("The word you entered, does not contain only letter");
for(i=0; i<100; i++)
word1[i] =0;
return 0;
}
}
fputs("Enter the 2nd word: ", stdout);
fflush(stdout);
fgets(word2, sizeof word2, stdin);
for (j=0; j<100; j++)
{
if(isalpha(word2[j]))
{
word2[j] = toupper(word2[j]);
}
else;
{
printf("The word you entered, does not contain only letter");
for(j=0; j<100; j++)
word2[j] =0;
return 0;
}
}
if (strlen(word1) != strlen(word2))
{
printf("The words are not anagrams.\n");
}
printf("word1 = %s\n", word1);
printf("word2 = %s\n", word2);
}