c语言写出输入一行电报文字,将字母变成其下一字母(如’a’变成’b’……’z’变成’a’其它字符不变)。
时间: 2023-10-29 15:04:22 浏览: 44
好的,下面是用C语言实现将输入的一行电报文字进行字母位移的代码:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char c;
while ((c = getchar()) != '\n') { // 逐个读入字符,直到读到回车符
if (isalpha(c)) { // 如果是字母
if (c == 'z') { // 如果是小写字母z
c = 'a'; // 则将其变成小写字母a
} else if (c == 'Z') { // 如果是大写字母Z
c = 'A'; // 则将其变成大写字母A
} else {
c++; // 否则将字母位移一位
}
}
putchar(c); // 输出字符
}
putchar('\n'); // 输出回车符
return 0;
}
```
具体实现方法是,逐个读入字符,如果是字母,则判断它是否是小写字母z或大写字母Z,如果是,则将其分别变成小写字母a和大写字母A,否则将其字母位移一位。最后输出修改后的字符。
相关问题
用c语言写一段输入一行电报文字,将字母变成其下一字母(如’a’变成’b’……’z’变成’a’其它字符不变)。的代码
好的,以下是你需要的代码:
```c
#include <stdio.h>
int main() {
char str[100];
int i = 0;
printf("请输入一行电报文字:");
fgets(str, 100, stdin); // 读入一行字符
while (str[i] != '\0') {
if (str[i] >= 'a' && str[i] < 'z') {
str[i] = str[i] + 1; // 小写字母往后移一位
} else if (str[i] == 'z') {
str[i] = 'a'; // 字母 z 变为 a
} else if (str[i] >= 'A' && str[i] < 'Z') {
str[i] = str[i] + 1; // 大写字母往后移一位
} else if (str[i] == 'Z') {
str[i] = 'A'; // 字母 Z 变为 A
}
i++;
}
printf("加密后的电报文字为:%s", str);
return 0;
}
```
以上代码实现了输入一行电报文字,并将其中的字母往后移一位,其它字符不变,最后输出加密后的电报文字。
输入一行电报文字,将字母变成其下一字母(如’a’变成’b’……’z’变成’a’其它字符不变)。给出c语言代码
Certainly! Here is a Python code that will take an input string and replace each letter with the next letter in the alphabet (e.g. 'a' becomes 'b', 'b' becomes 'c', etc.):
```
def replace_letters(input_string):
output_string = ''
for letter in input_string:
if letter.isalpha():
if letter == 'z':
output_string += 'a'
elif letter == 'Z':
output_string += 'A'
else:
output_string += chr(ord(letter) + 1)
else:
output_string += letter
return output_string
```
Here's how you would use this function:
```
input_string = "hello world"
output_string = replace_letters(input_string)
print(output_string)
```
This would output:
```
ifmmp xpsme
```
I hope that helps! Let me know if you have any questions.