0

I'm relatively inexperienced at programming, but I thought I understood how to use command-line prompts well enough to try the substitution problem. I'm having an issue with isalpha that I don't understand, and couldn't find explained here.

When I run this with the regular, sequential alphabet as the command-line prompt, it returns:

A entered.
Key must contain only letters.

It's the same result with lowercase letters as well. I've posted the program right up until the problem area starts.

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./substitution <key>\n");
        return 1;
    }

    int n = strlen(argv[1]);

    if (n != 26)
    {
        printf("Key must contain twenty-six characters.\n");
        return 1;
    }

    // this is where it stops working

    for (int i = 0; i < n; i++)
    {
        char c = (argv[1][i]);
        if ( isalpha ( c ) != 'true' )
        {
            printf("%c entered.\n", c);
            printf("Key must contain only letters.\n");
            return 1;
        }
     }

Why isn't this function recognizing letters? What does it think I'm doing instead? In the rest of my code for this problem, I keep switching between ASCII-based implementations and functions like toupper, so I think figuring this out here will help me approach it more consistently.

Thanks very much for your time.

2 Answers 2

1

You've run into a common newbie problem. The issue lies in this code:

    if ( isalpha ( c ) != 'true' )

The issue is with the return value of isalpha and what you're comparing it to. Let's handle the latter first. Putting true in single quotes is a problem. I'm not even sure what that will evaluate to. IF you want to compare something to true or false, use the keyword true, or false, but most importantly, without quotes.

The other issue here is what is returned by isalpha and how c evaluates integers as bools. C will always evaluate 0 as false and any non-zero (including negatives) as true. With that in mind, what does isalpha return?

Isalpha will always return 0 if false. If true, though, it gets interesting. Each of the functions in the issomething() family will return a different number that is a power of two, depending on which function it is. You should always assume that it doesn't return 1.

Now, take all that and add this. C will always evaluate true as 1 (and false as 0). So, if isalpha returns 64 and true evaluates as 1, C will compare them in the above statement and the result evaluates as false.

If you really want to use these functions, the best way is this:

if(isalpha(something)) 

or

if(!isalpha(something))

These no longer depend on a secondary comparison to true (which is 1) or false ( 0 ).

IF this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Thank you for that lecture. And again something learned :-)
    – ALL
    Commented May 15, 2020 at 16:59
  • This was actually broadly applicable to all the other boolean operations in my program! Thanks a lot!
    – dubflicker
    Commented May 15, 2020 at 20:51
1

I am not a Pro, too, but the first thing I see is the "isalpha(c) != "true" -> When I recall this pset correctly, isalpha() does not return "false" or "true" but 0 or 1. But if the statement would be wrong the compiler would complain. So it must be a logical issue. However, that would be my first approach to solve this.

Currently it looks like (at least for me), that your program checks, if isalpha(c) (respectively the stored value/letter) is 1 or 0 (int!) and then compares it with a string ("true") - that might work for the compiler but not for the code's functionality.

1
  • It looks like you're right! It worked when i declared bool d = isalpha(c); and then changed the argument to if ( d != 1 ). For some reason, I was having trouble finding coherent documentation or tutorials on bool, so this was great. Thanks!
    – dubflicker
    Commented May 15, 2020 at 18:30

You must log in to answer this question.

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