0

My code works properly, except that it doesn't reject numeric key. I try using an if statement to validate input key as follow:

int main (int argc, string argv[]) { if((argc != 2) || (isalnum(argv[1])))

{
    printf("type: ./vigenere key\n");
    return 1;
}

What am I doing wrong? error message: \ expected output, not standard error of "/opt/sandbox50/bin/run.sh: line 31: 277..." https://sandbox.cs50.net/checks/ec152b9d69f345799f07e92002b15dee

0

isalnum() checks for an alphanumeric character, argv[1] is not a character but a string (exactly a pointer to string),the right way would iterate over the length of argv [i] to check each character individually, we can use the array notation and write key [i]. On the other hand isalnum is equivalent to (isalpha(c) || isdigit(c)). So if you say

if(isalnum(key[i]))

you are rejected alphabetic characters, surely you want to do

if(!isalpha(key[i]))
0

Remember argv[1] points to the beginning of a string, or a char array more specifically, so you cannot pass that as an argument to isalnum(). To better understand this you could picture it as a 2D array in which argv[1][0] is the first char in your array, argv[1][1] is the second one, and so on.

You must log in to answer this question.

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