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);  
	
}
if (isalpha(charValue))
{
    // charValue is an alpha
}
else
{
    // charValue is not an alpha
}

Returns TRUE if charValue is A-Z or a-z
Returns FALSE if not.

Actually here's the whole code I have so far:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
 
int main()
{
   char word1[100], word2[100];
   int count[26];

   int i, j, k;

   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");
      return 0;
   }
	
   for (k=0; k<26; k++) 
   {
      if (word1[k] >= 'A' && word1[k] <= 'Z')
      {
	 count[word1[k]-'A']++;
      }
      else
      {
	 printf("The words are not anagrams.\n");
	 return 0;
      }
   }

   for (k=0; k<26; k++) 
   {
      if (word2[k] >= 'A' && word2[k] <= 'Z') 
      {
	 if (count[word2[k]-'A'] == 0)
	 {
	    printf("The words are not anagrams.\n");
	    return 0;
	 }
	 count[word2[k]-'A']--;
      } 
      else 
      {
	 printf("The words are not anagrams.\n");
	 return 0;
      }
   }

   printf("The words are anagrams.\n");
   return 0;
}
if (isalpha(charValue))
{
    // charValue is an alpha
}
else
{
    // charValue is not an alpha
}

Returns TRUE if charValue is A-Z or a-z
Returns FALSE if not.

I'm confused then. Why why wont it work the way I have it evaluating the char at word1?

Try outputting the characters in hex just before the test, and which block was entered.

Try outputting the characters in hex just before the test, and which block was entered.

I'm sorry I'm afraid I don't understand what you mean by outputting in HEX. I'm rather new at this so you my have to dumb it down.

Try outputting the characters in hex just before the test, and which block was entered.

Never mind. I get it now. Thank you.

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.