|
发表于 2023-11-26 13:26:02
|
显示全部楼层
感谢提供代码。我注意到你的代码存在一些问题。首先,你定义了一个字符变量`a`,应该使用字符数组来存储输入的字符串。其次,你在循环中使用`gets()`函数获取字符串,这个函数已经被标记为不安全的,建议使用更安全的`fgets()`函数。此外,在你的循环中,没有对输入字符串进行加密操作。
下面是修改后的代码示例:
- #include <stdio.h>
- #include <string.h>
- void encrypt_string(char *str, int n) {
- int len = strlen(str);
- for (int i = 0; i < len; i++) {
- if (str[i] >= 'a' && str[i] <= 'z') {
- // 小写字母处理
- str[i] = ((str[i] - 'a') + n) % 26 + 'a';
- } else if (str[i] >= 'A' && str[i] <= 'Z') {
- // 大写字母处理
- str[i] = ((str[i] - 'A') + n) % 26 + 'A';
- }
- }
- }
- int main() {
- int n;
- char str[100];
- while (1) {
- scanf("%d", &n);
- getchar();
- fgets(str, sizeof(str), stdin);
- int len = strlen(str);
- if (str[len - 1] == '\n') {
- str[len - 1] = '\0'; // 去掉输入字符串末尾的换行符
- }
- if (n == 0) {
- break;
- }
- encrypt_string(str, n);
- printf("%s\n", str);
- }
- return 0;
- }
复制代码
在上面的示例中,我们定义了`encrypt_string`函数来进行加密操作。在循环中,我们使用`fgets()`函数获取输入的字符串,并去掉末尾的换行符。然后,我们调用`encrypt_string`函数对字符串进行加密,并打印加密后的结果。如果输入的加密常数为0,则退出循环。
希望这个示例能够帮助你解决问题。如果还有其他疑问,请随时提问。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。 |
|