I'm currently on vigenere, and trying to make sure that the keyword does not contain any non-alpha characters. If there's a non-alpha character, the program should return 1 to main. Here is my code:

for (int i = 0, j = strlen(argv[1]); i < j; i++)
        if (isalpha(argv[1][i]) != 1)
            printf("Error - please make sure you type in your cipher as just one string.\n");
            return 1;

Its not working - the program doesn't exit if i give a keyword with non-alpha characters, and my guess is because it's returning 1 within the for loop, so the loop keeps running.

How can I break out of the loop to return 1 if the condition is true?

1 Answer 1


Your return line is fine. That will indeed end the program if your if statement is true.

The issue is that isalpha returns a nonzero value if the character is alphabetic, and a zero value if not.

  • Also, isalpha doesn't return a bool. The number returned when the letter is an alphabetic is not 1. If not an alpha, 0 will definitely be returned.
    – Cliff B
    Sep 30, 2017 at 20:22

You must log in to answer this question.

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