1

I'm using the construct

  if(isalpha(argv[1][i]) == 0) 

to determine if the chars are alphabetical in argv[1] with a for loop. The program compiles, however, isalpha is not working. If I enter, for instance, ./vigenere abc1, the program moves on to ask for a string as though argv[1] is valid, which of course messes up the output. Specifically, this is the code:

for (a = 0, b = strlen(key); a < b; a++)

{
    if isalpha(key[a] == false)

    {
        printf ("%c: Invalid key, try again\n", key[a]);
         return 1;
    }

   else

   {
     printf ("Validating key.\n");
   }

}

I'm sure I'm just doing something stupid, but it's still frustrating and I appreciate any input on this.

Thank you.

3
  • 1
    ` if isalpha(key[a] == false)` is missing a pair of parentheses. Is this a copy error when you posted it above?
    – Cliff B
    May 17 '16 at 19:29
  • Actually it was a copy error I made here because the code compiled and I figured out the way to check argv[1] with isalpha and now it works. I assigned argv[1] to a 'char *' variable and then checked the variable with isalpha. Worked like a charm. Thanks for your answer. May 19 '16 at 16:04
  • @lury always Format your code properly.
    – Wafeeq
    Jun 10 '16 at 16:21
0

"The values returned are nonzero if the character c falls into the tested class, and a zero value if not." We can use that to build the if condition, the correct way would be:

if (! isalpha (key [a]))

which it is equivalent to

if (isalpha (key [a]) == 0)

but shorter and elegant

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .