C语言用循环实现:输入一个字符串,以回车符结束,将该字符串中每个字符进行移位加密后输出。 加密规则:每个字母往后移3位,x,y,z分别变成a,b,c,其他字母不变。
时间: 2024-04-30 22:22:02 浏览: 34
下面是使用循环实现该功能的C语言代码:
```c
#include <stdio.h>
int main() {
char str[100]; // 用于存储输入的字符串
printf("请输入字符串:");
fgets(str, 100, stdin); // 读取输入的字符串
// 对每个字符进行移位加密并输出
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = (str[i] - 'a' + 3) % 26 + 'a';
} else if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = (str[i] - 'A' + 3) % 26 + 'A';
}
printf("%c", str[i]);
}
return 0;
}
```
注意,我们使用了 `fgets` 函数来读取用户输入的字符串,因为它可以在读取完字符串后自动添加一个回车符(`\n`),这样我们就可以利用字符串的结束标志符 `\0` 来判断字符串是否已经结束。在循环中,我们首先判断字符是否为小写字母或大写字母,然后根据加密规则进行移位加密,并输出加密后的字符。
相关问题
用c语言用循环实现:输入一个字符串,以回车符结束,将该字符串中每个字符进行移位加密后输出。 加密规则:每个字母往后移3位,x,y,z分别变成a,b,c,其他字母不变。
好的,以下是使用循环实现对字符串中每个字符进行移位加密的示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100]; // 定义字符串
int len; // 字符串长度
// 输入字符串
printf("Please enter a string:\n");
scanf("%[^\n]", str);
len = strlen(str);
// 对每个字符进行移位加密
for (int i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z') { // 小写字母
str[i] = (str[i] - 'a' + 3) % 26 + 'a';
} else if (str[i] >= 'A' && str[i] <= 'Z') { // 大写字母
str[i] = (str[i] - 'A' + 3) % 26 + 'A';
}
}
// 输出加密后的字符串
printf("Encrypted string: %s\n", str);
return 0;
}
```
运行以上代码,输入一个字符串后,输出加密后的字符串。例如,输入以下字符串:
```
Hello, World!
```
则输出结果为:
```
Encrypted string: Khoor, Zruog!
```
其中,所有字母都往后移动了3位,而符号和空格不受影响。希望能够帮到您,如果您还有其他问题,可以继续提出。
c语言用循环实现:输入一个字符串,以回车符结束,将该字符串中每个字符进行移位加密后输出。 加密规则:每个字母往后移3位,x,y,z分别变成a,b,c,其他字母不变。
好的,这是一个关于程序设计的问题。以下是程序的实现方法:
#include <stdio.h>
#include <string.h>
int main()
{
char str[100], encrypted_str[100];
int i, len;
printf("请输入一个字符串:\n");
gets(str);
len = strlen(str);
for (i = 0; i < len; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
encrypted_str[i] = str[i] + 3;
if (encrypted_str[i] > 'z') {
encrypted_str[i] -= 26;
}
} else if (str[i] >= 'A' && str[i] <= 'Z') {
encrypted_str[i] = str[i] + 3;
if (encrypted_str[i] > 'Z') {
encrypted_str[i] -= 26;
}
} else {
encrypted_str[i] = str[i];
}
}
encrypted_str[i] = '\0';
printf("加密后的字符串为:%s\n", encrypted_str);
return 0;
}
以上程序用到了循环语句、条件语句和字符串处理函数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)