输入字符串对它进行反序,用c语言
时间: 2023-05-16 18:05:19 浏览: 57
C语言中可以使用以下代码来对字符串进行反序:
```
#include <stdio.h>
#include <string.h>
void reverse_string(char *str) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
int main() {
char str[] = "Hello, World!";
printf("原字符串: %s\n", str);
reverse_string(str);
printf("反转后的字符串: %s\n", str);
return 0;
}
```
输出结果:
```
原字符串: Hello, World!
反转后的字符串: !dlroW ,olleH
```
相关问题
使输入的字符串按反序c语言
以下是一个将输入字符串反序存放的C语言程序:
```c
#include <stdio.h>
#include <string.h>
void reverse(char *str) {
int len = strlen(str);
char temp;
for (int i = 0; i < len / 2; i++) {
temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
reverse(str); printf("反序后的字符串为:%s\n", str);
return 0;
}
```
输入一串字符,将字符串中的字符反序输出。用c语言回答
以下是用C语言实现字符串反序输出的代码示例:
```c
#include <stdio.h>
#include <string.h>
void reverseString(char* str) {
int length = strlen(str);
int i, j;
char temp;
for (i = 0, j = length - 1; i < j; i++, j--) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
reverseString(str);
printf("反序输出的字符串为:%s\n", str);
return 0;
}
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![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)