Caesar Cipher不使用空格

时间:2014-11-01 01:58:24

标签: c encryption cs50 caesar-cipher

我很难让空间正确显示。以下是打印内容的输出:

./CaeserC 13[A
Encrypt message by 13[A spaces
Please insert your message:
Hello World 
Your message is: Hello World
Your encrypted message is:Uryybadbeyq

需要是Uryyb Jbeyq

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

int main (int argc, string argv[])
{
    string kinp = argv[1];
    int k = atoi(argv[1]);
    printf("Encrypt message by %s spaces\n", kinp);
    printf("Please insert your message:\n");
    string p = GetString();
    int lettern;
    printf("Your message is: %s\n", p);
    printf("Your encrypted message is:");
    for (int i=0, n=strlen(p); i<n; i++)
        {  
            lettern=p[i];
            //printf("%i \n", lettern);
        }
    if (lettern==32)
    {
        printf("%c", ' ');
    }
    if (lettern>= 65 && lettern<=90)
    {
    for (int i=0, n=strlen(p); i<n; i++)
        {  
            printf("%c", ((p[i]+k-65)%26)+65);
        }
    }
    if (lettern>= 97 && lettern<=122)
    {
        for (int i=0, n=strlen(p); i<n; i++)
        {
            printf("%c", ((p[i]+k-97)%26)+97);
        }
    }

    printf("\n");
}

2 个答案:

答案 0 :(得分:2)

printf("Your encrypted message is:");
for (int i=0; p[i]; i++){
    if(isupper(p[i]))
        putchar((p[i]+k-'A')%26 +'A');
    else if(islower(p[i]))
        putchar((p[i]+k-'a')%26 +'a');
    else
        putchar(p[i]);
}

答案 1 :(得分:0)

只需添加一个简单的if循环检测是否是字符是空格(“”),就好像我认为这是用于您的受控评估是正确的,他们不希望空格被移动。