C语言编写一个函数fun(char* s),该函数的功能是把字符串中的内容逆置
时间: 2024-04-13 14:29:25 浏览: 115
您好!以下是一个使用C语言编写的函数fun(char* s),该函数可以将字符串中的内容进行逆置:
```c
#include <stdio.h>
#include <string.h>
void fun(char* s) {
int length = strlen(s);
int i, j;
char temp;
for (i = 0, j = length - 1; i < j; i++, j--) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
int main() {
char str[] = "Hello, World!";
printf("Original string: %s\n", str);
fun(str);
printf("Reversed string: %s\n", str);
return 0;
}
```
在上述代码中,我们首先使用`strlen`函数获取字符串的长度,然后使用两个指针`i`和`j`分别指向字符串的首尾。通过交换指针所指向位置的字符,我们可以将字符串内容进行逆置。在`main`函数中,我们定义了一个字符串`str`,并在调用`fun`函数后打印逆置后的字符串。
运行上述代码,输出将会是:
```
Original string: Hello, World!
Reversed string: !dlroW ,olleH
```
希望这能帮到您!如果您有任何其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.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)