用c实现如下加密规则,内容遇到英文字符则往后移5位(比如a,加密后就变为f.请编写程序,实现文本加密功能,输入“Hello world!”,将加密结果输出。
时间: 2024-03-17 09:40:16 浏览: 19
好的,根据您的要求,我用 C 编写了一个文本加密程序。以下是代码:
```c
#include <stdio.h>
#include <string.h>
void encrypt(char* text)
{
int i, length = strlen(text);
for (i = 0; i < length; i++)
{
char c = text[i];
if (c >= 'A' && c <= 'Z')
{
c = (c - 'A' + 5) % 26 + 'A';
}
else if (c >= 'a' && c <= 'z')
{
c = (c - 'a' + 5) % 26 + 'a';
}
text[i] = c;
}
}
int main()
{
char text[] = "Hello world!";
encrypt(text);
printf("%s", text);
return 0;
}
```
程序的逻辑与 Python 版本类似,也是遍历输入的文本中的每个字符,如果是英文字母则按照规则进行加密,否则不做处理。其中,`strlen()` 函数可以获取字符串的长度。
运行程序后,输出的加密结果为:`Mjqqt btwqi!`。