Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

Caesar cipher in C working for lower not upper

Cipher works for islower portion but not isupper portion. For instance if I give a key of 3 and enter I like pie!! to be encrypted, I get O olnh slh!! I also tried HELLO and got NKRRU. The isupper portion is also returning punctuation instead of just letters. I also have not figured out why the original message is being altered to match the cipher message.

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int main (int argc, string argv[])
{
    /*
    Get key from user at command line
    Get plaintext from user
    Use key to encipher text: c[i] = (p[i] + k)%26
    Print ciphered message
    */        
    string message, cipher;
    int key;

    // command line, if user doesn't enter 2 arguments return 1 and request a valid 
    //encryption key and rerun.
    if (argc != 2)
        {
        printf("Please enter a valid encryption key and rerun program.
");
        return 1;
        }
    else
        {
        key = atoi(argv[1]);
        }


    printf("Enter the message you wish to encrypt.
");
    message = GetString();
    cipher = message;
    int length = strlen(message);

    for ( int i = 0; i < length; i++)
        {
        if (isalpha(message[i]))
            {
            if (isupper(message[i]))
                {
                cipher[i] = (message[i] - 'A' + key) % 26 + 'A';
                }
            else (islower(message[i]));
                {
                cipher[i] = (message[i] - 'a' + key) % 26 + 'a';
                }
            }
        else continue; //message[i] contains punctuation or a space
        } 
         printf("Your original message was..
");
         printf("%s
", message);
         printf("The encrypted message is...
");
         printf("%s
", cipher);         
         return 0;            
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Typo and missing if per @interjay.

Change

else (islower(message[i]));

to

//                           v
else if (islower(message[i]))
// or simply 
else  // Since `message[]` is an alpha, but not upper

With the error, when text was uppercase, both cipher[i] = (message[i] - 'A' ... and cipher[i] = (message[i] - 'a' ... occurred. Given cipher = message, the cipher was applied twice.


@keshlam point about the missing buffer is a significant issue. But I wonder what type string is. Is this some sort of C++ lite string? If it is a char *, code could use cipher = strdup(message); or

cipher = malloc(length + 1);
if (cipher === NULL) Handle_OutOfMemeory();
cipher[length] = '';
for ( int i = 0; i < length; i++) 
  ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...