#include <stdio.h>
int main()
{
char msg[31] = {'\0'};
char encrypted[31] = {'\0'};
int key;
printf("Please enter a message under 30 characters: ");
fgets(msg, 31, stdin);
printf("Please enter an encryption key: ");
scanf("%d", &key);
int i = 0;
if (msg[i] && (('a' >= msg[i] && msg[i]>= 'z') || ('A' >= msg[i] && msg[i] >= 'Z')))
{
i++;
} else {
while (msg[i] && (('a' <= msg[i] && msg[i]<= 'z') || ('A' <= msg[i] && msg[i] <= 'Z')))
{
encrypted[i] = (msg[i] + key);
i++;
}
}
printf("%s\n", msg);
printf("%d\n", key);
printf("%s\n", encrypted);
}
我的代码可以工作,但我不知道如何让递增忽略特殊字符和空格。另外,我如何使用 % 循环回 'a' 和 'A' 以保持消息中的所有大写字母相同?