0

I have run the debugger tested different combinations in my code, but I can't figure out why nothing is functioning. I input abc, I output abc.

My program seems to determine all characters from the string as non alphabetical, so it just passes down to "printf("%c", p[i]);".

I'm stumped, thank you for your help.

 for (int i = 0, n = strlen(p); i < n; i++)
{
    if (isalpha(p[i]) == true)
    {
        if (isupper(p[i]) == true)
        {
            //compute algorithm for upper case letters
            p[i] = (((p[i] - 65) + k) % 26) + 65;

            printf("%c", p[i]);
        }
        else
        {
            //compute algorithm for lower case letters
            p[i] = (((p[i] - 97) + k) % 26) + 97;

            printf("%c", p[i]);
        }
    }
    else
    {
        //print any non alphabetic characters
        printf("%c", p[i]);
    }
}
1

The problem is surely in the return value of the isalpha function and the value of true, for what the condition is true both values must be equal, which apparently does not happen, keep in mind that isalpha, isupper etc return a value Nonzero in case of success, not necessarily a value of 1, to avoid this problem we can write simply:

if (isalpha(p[i]))

Which means that the if condition is true if the returned value is not zero

1
  • Thank you so much. Works perfectly without ==true. Mar 23 '17 at 17:26

You must log in to answer this question.

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