Caesar Cipher简单程序

-1 c encryption

我从这里获取了一个简单的Caesar密码的代码,我修改了它,以便用户定义密钥.但每次我试图运行程序时程序都会崩溃.

#include <stdio.h>

int main() 
{
    char array[100], cipher[100];
    int c=0, x=0, y=0;
    int z;
    printf("This Program will encrypt according to your needs\n");
    printf("Enter the cipher key\n");
    scanf("%d",&z);
    printf("Enter the sentence");
    while((c=getchar()) != '\n')
    {
        array[x++]=(char)c;
        cipher[y++]=(char)(c+z);
    }

    array[x]=0;
    cipher[y]=0;

    printf("%s\n",cipher);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

avm*_*han 5

它没有崩溃.while循环立即结束,因为'\n'在scanf之后位于输入缓冲区中,并且首先读取它