I am working on ceasar cipher for days and I don't know what I am doing wrong. My code is compiling. There are no errors anymore. But there is something wrong with my formula I guess. What's wrong with my formula?

for(int i = 0; i < length; i++)  
    char current = message[i];
    char cipher[i];
    char message[i];
    if (isalpha(current))
        if (islower(current))
            cipher[i] = ((message[i] - 'a') + key) % 26 + 'a'; 
            printf("%c", cipher[i]); 
        else if (isupper(current))
            cipher[i] = ((message[i] - 'A') + key) % 26 + 'A';
            printf("%c", message[i]);


Take a look at both of your printf statements. Should they be the same? If the letter is uppercase, are you looking to print out just the uppercase letter without ciphering?

If the current character from your user's input is not a letter, it should be printed out, not just skipped over. To accomplish this, try adding an else statement to your outermost if.

Lastly, why are you initializing a variable current and assigning it to message[i] and then initializing message[i]?

Your actual equations look great though! Good work.

| improve this answer | |
  • Thank you so much you gave me the last push till the end! – LvdD Oct 21 '15 at 12:21

You must log in to answer this question.

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