代码如下(用Visual Studio 2022写的):
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
void encrypt(char* text) {
int i = 0;
while (text[i] != '\0') {
if (isalpha(text[i])) { // 判断是否为字母
if (islower(text[i])) { // 判断是否为小写字母
text[i] = toupper(text[i]); // 转换为大写字母
text[i] = (text[i] - 'A' + 4) % 26 + 'A'; // 处理大写字母的加密
text[i] = tolower(text[i]); // 转回小写字母
}
else { // 处理大写字母的加密
text[i] = (text[i] - 'A' + 4) % 26 + 'A';
}
}
i++;
}
}
int main() {
char text[100];
printf("请输入要加密的文本:");
scanf_s("%s", text, sizeof(text)); // 使用 scanf_s 替代 scanf ,输入要加密的明文
encrypt(text);
printf("加密后的文本:%s\n", text);
return 0;
}
运行结果如下: