Question

This is my code for the ROT13 cipher. It works with upper case but when i give in hello as the input , it returns [X__b can anyone help me out here ...

void encrypt(char *cipher,int key) 
{
    int i = 0;
    while(cipher[i] != '\n') 
    {
        if(isalpha(cipher[i]))
        {
            if((cipher[i] + key) > 'Z' || (cipher[i] + key) > 'z')
            {
                cipher[i] = cipher[i] + key - 26;
            }
            else if((cipher[i] + key) >= 'a' && (cipher[i] + key) <= 'z')
            {
                cipher[i] = cipher[i] + key;
            }
            else if((cipher[i] + key) >= 'A' && (cipher[i] + key) <= 'Z')
            {
                cipher[i] = cipher[i] + key;
            }
        }
        i++;
    }
}
Was it helpful?

Solution

This condition

f((cipher[i] + key) > 'Z' || (cipher[i] + key) > 'z')

will be true for ALL lowercase letters, the second part (cipher[i] + key) > 'z' being redundant

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top