0

I'm trying to solve Caesar cypher ploblem from the course by using an if statement inside another if statement (Nested IF statement? apologies if I'm using the term incorrectly.)

My draft attempt in constructing the cypher.

for(int i = 0, n = strlen(text); i < n; i++)
{

    if(isupper(text[i]) == true)
    {
        //Solves space problem
        if ((int) text[i] == 32)
        {
            printf(" ");
        }
        //Caesar in action
        else if ((int) text[i] != 32)
        {
            int alpha = (int) text[i] - 65;
            new_value[i] = ((alpha + 13) % 26) + 65;
            printf("%c",(char) new_value[i]);
        } 
    }
    else if (islower(text[i]) == true)
    {
        //Solves space problem
        if ((int) text[i] == 32)
        {
            printf(" ");
        }
        //Caesar in action
        else if ((int) text[i] != 32)
        {
            int alpha = (int) text[i] - 97;
            new_value[i] = ((alpha + 13) % 26) + 97;
            printf("%c",(char) new_value[i]);
        }  
    }

}

As you can see I'm using neseted IF statements. My output in compiler, after user submits the string, is empty. Is it because using an IF statement within an IF statement is invalid syntax?

  • What error are you getting? It is unlikely that this problem is the result of a nested if-statement. – NullityNull Aug 14 '16 at 18:46
1

You have made a mistake that is very frequent among novice programmers, you check for the returned bool using ==.

You should instead use something like:

if (islower(text[i]))
{
    // do things...
}

The reason your code produces no output, is the fact that islower() doesn't return a bool, but an int. Behind the scenes, true == 1 and false == 0, but according to cppreference.com:

int islower( int ch );

Checks if the given character is classified as a lowercase character according to the current C locale. In the default "C" locale, islower returns true only for the lowercase letters (abcdefghijklmnopqrstuvwxyz).

If islower returns true, it is guaranteed that iscntrl, isdigit, ispunct, and isspace return false for the same character in the same C locale.

The behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF.

Parameters ch - character to classify

Return value Non-zero value if the character is a lowercase letter, zero otherwise.

So in success, islower() return a non-zero value, which doesn't have to be 1, which is the same as true.

One more mistake you have is that islower() and isupper() return 0 (false) for the space character, so maybe you should check for space outside the nested ifs and then check for lowercase/uppercase.

Also, why do you use alpha + 13? What's that 13?

| improve this answer | |
  • 1
    Ah! In my defence this was my second week. This was just a draft attempt. The 'alpha + 13' an arbitrary key used, a test value more like. Thanks for the informative answer. – MrDantab Aug 14 '16 at 19:47
  • If this answers your question, please mark it as accepted. – ChrisG Aug 15 '16 at 13:27

You must log in to answer this question.

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