c语言凯撒密码、字母倒排序
时间: 2023-12-01 20:04:09 浏览: 42
凯撒密码用c语言来实现
凯撒密码:
```c
#include <stdio.h>
int main() {
char str[100], cipher[100];
int key;
printf("请输入需要加密的字符串:");
fgets(str, 100, stdin);
printf("请输入密钥:");
scanf("%d", &key);
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
cipher[i] = (str[i] - 'a' + key) % 26 + 'a';
} else if (str[i] >= 'A' && str[i] <= 'Z') {
cipher[i] = (str[i] - 'A' + key) % 26 + 'A';
} else {
cipher[i] = str[i];
}
}
printf("加密后的字符串为:%s\n", cipher);
return 0;
}
```
字母倒排序:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("请输入一个字符串:");
fgets(str, 100, stdin);
int len = strlen(str);
for (int i = len - 2; i >= 0; i--) { // 注意要减去1
printf("%c", str[i]);
}
printf("\n");
return 0;
}
```
阅读全文