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.