0

hi i need some help if anyone has an answer! I'm trying to make sure that every character in a string is an alphabetical char. So i use isalpha() to check char by char. Is there a function where I can make it so : if ALL characters in string are alphabetical, do this, or if else return 1 ? my issue is i created a loop where it does this one char at a time so the thing that i only want done once, is happening once for every char. i hope this makes sense here's what my code looks like.

for(int g = 0, h = strlen(k); g < h;g++)
{
    if (isalpha(argv[1][0]))
    {
        printf("Please enter some text to encrypt.\n"); // - asks user for input
        //string n = GetString();
    }

    else
    {
        printf("you must enter one key made up of a keyword. alphabetical characters only.\n");
        return 1;           
    }
}
0

In your sample, it looks like you have a couple of issues going on. First, it appears that you're trying to validate the cipher key, while at the same time, asking for text to encrypt. (There's also an issue with the "isalpha" call hardcoded as argv[1][0]. I'm guessing that typo was supposed to be argv[1][g] in your code. I'm also assuming that k is argv[1].)

OK, now, once you separate those two tasks in your code, here's the next issue. Your loop is asking for input each time you check a character in the string that you are trying to verify as all alpha.

The fix is simple. Move the code that asks for input (printf and getstring) outside of the loop. It should not be executed there. It belongs later in the program, assuming that you are checking the key here. No need to ask for input before you validate the key. If it is checking the input string, move it before the check loop.

Next, alter your test to something a little more efficient, like this pseudocode:

for(...each character...)
{
    if(not alpha)
    {   
         print error message;
         return 1;
    }
}

This should get you going.

| improve this answer | |

You must log in to answer this question.

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