c中的加密不起作用

Use*_*und 2 c encryption

在下面的程序中,我想加密一个句子.然而,当空间到来时它会提供不完整的输出.任何帮助都非常有用.

#include<stdio.h>
#include<string.h>
main()
{
    printf("Enter a string : ");
    char c[100];
    scanf("%s",c);
    int i;
    for(i=0;i<100;i++)
    {
        if((c[i]>='A'&&c[i]<='Z')||(c[i]>'a'&&c[i]<'z'))
        {
            c[i]+=13;
            if(!(c[i]>='A'&&c[i]<='Z')||(c[i]>'a'&&c[i]<'z'))
            c[i]-=26;
        }
    }
    printf("Encrypted string is : %s\n",c);
}
Run Code Online (Sandbox Code Playgroud)

提前致谢

Dav*_*eri 5

scanf停止在空白处阅读,改为fgets:

fgets(c, sizeof c, stdin);
Run Code Online (Sandbox Code Playgroud)

您可以使用以下命令跳过尾随换行符:

char c[100], *p;
fgets(c, sizeof c, stdin);
if ((p = strchr(c, '\n')) != NULL) { 
    *p = '\0'; /* remove newline */
}
Run Code Online (Sandbox Code Playgroud)

另外,考虑使用

if (isalpha((unsigned char)c[i]))
Run Code Online (Sandbox Code Playgroud)

代替

if((c[i]>='A'&&c[i]<='Z')||(c[i]>'a'&&c[i]<'z'))
Run Code Online (Sandbox Code Playgroud)

不要忘了包括 <ctype.h>