输入一个字符串,将该字符串逆序后输出。 输入: ab cd efg 输出: gfe dc ba c语言
时间: 2024-02-18 21:59:22 浏览: 146
好的,您可以使用以下 C 语言代码实现该功能:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
fgets(str, 100, stdin); // 从标准输入读取字符串,以避免不必要的输入错误
int len = strlen(str);
for (int i = len - 1; i >= 0; i--) {
printf("%c", str[i]);
}
return 0;
}
```
这个程序会从标准输入读取一个字符串,然后计算字符串的长度,最后按照倒序输出字符串的每个字符。
相关问题
c语言输入一个字符串,将该字符串逆序后输出。 输入: ab cd efg 输出: gfe dc ba
下面是一个简单的C语言程序,实现输入一个字符串,将该字符串逆序后输出。
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
int len = strlen(str);
printf("逆序后的字符串为:");
for (int i = len - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n");
return 0;
}
```
在上面的代码中,我们首先定义了一个字符数组`str`,用于存储输入的字符串。然后使用`scanf`函数从标准输入中读取一个字符串,存储到`str`中。接着,使用`strlen`函数计算出字符串的长度。最后,使用`for`循环从字符串的末尾开始,逐个输出字符,即可实现字符串的逆序输出。
用c语言输入一个字符串,将该字符串逆序后输出。 输入: ab cd efg 输出: gfe dc ba
好的,可以使用以下代码实现字符串逆序输出:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str); // 读入一个字符串
int len = strlen(str);
for (int i = len - 1; i >= 0; i--) { // 从后往前遍历字符串
printf("%c", str[i]); // 输出字符
}
printf("\n");
return 0;
}
```
输入字符串时使用 `scanf("%s", str)`,使用 `strlen` 函数获取字符串长度,并从后往前遍历字符串,输出每个字符即可。注意,使用 `%s` 读入字符串时,会自动在字符串末尾添加一个空字符 `\0`,所以字符串的长度需要减一。
阅读全文
相关推荐









