[Solved] How to go back from ‘z’ to ‘a’ in casear cipher encryption


You won’t get ‘a’ because your second if still runs. The first if changes the value to A or a and the second if adds the KEY value. If you want all z to become a and Z to become A you want to change this to an else if like this:

if(message[i] == 'z' || message[i] == 'Z')
{
    message[i] -= 25; 
}

else if( (message[i] >= 'a' && message[i] <= 'z') || (message[i] >= 'A' && message[i] <= 'Z') )
{
    message[i] += KEY;
}

I think you want something more like this though:

if((message[i] >= ('z' - KEY + 1) && message[i] <= 'z') || (message[i] > (('Z' - KEY + 1)) && message[i] < 'Z'))
{
    message[i] -= 26;
    message[i] += KEY;
}

else if((message[i] >= 'a' && message[i] <= 'u') || (message[i] >= 'A' && message[i] <= 'U'))
{
    message[i] += KEY;
}

That way whenever any value goes past Z or z it starts from the beginning of the alphabet.

3

solved How to go back from ‘z’ to ‘a’ in casear cipher encryption