-3

我一直在研究这个凯撒密码,这是我到目前为止所得到的,当程序运行时没有任何反应。

#include <stdio.h>
#define MAX 80

 main()
{
    int shift, num; 
    char ciph[MAX];
    printf("Enter the message to be encrypted: ");
    while ((ciph[num] = getchar()) != '\n'){  //loops until a blank space is read
        for(num=0; num<MAX; num++){ //scans characters into the array
        ciph[num] = getchar();
        }
    }
    printf("\nEnter the shift amout: ");
    scanf("%d", &shift); //reads the shift amount
    for (num = 0; num < MAX; num++) //shifts the letter
    {
        int c = ciph[num];
        if ('a' >= c && c <= 'z')
            c = ((c - 'a') + shift) % 26 + 'a';
        else if ('A' >= c && c <= 'Z')
            c = ((c - 'A') + shift) % 26 + 'A';
        else
        {

    }
    ciph[num] = c;
}
printf("%c", ciph[num]); //prints the encryption
}
4

2 回答 2

0

我不确定你的意图是什么,但是

'a' <= ciph

需要是

'a' <= ciph[SOMETHING]

ciph = (ciph - 'a')+shift) % 26 + 'a';

缺少一个“(”

于 2015-03-31T00:05:15.287 回答
0

我认为你应该把这个东西单独留下空白,但我想这取决于你。

//read the whole line

int i;
int len = strlen(line);
for (i = 0; i < len; i++)
{
    int c = line[i];
    if ('a' >= c && c <= 'z')
        c = ((c - 'a') + shift) % 26 + 'a';
    else if ('A' >= c && c <= 'Z')
        c = ((c - 'A') + shift) % 26 + 'A';
    else
    {
        //leave it
    }
    line[i] = c;
}

//write the whole line
于 2015-03-31T00:19:28.153 回答