c语言实现字符串翻转
时间: 2023-11-25 17:06:27 浏览: 73
C语言下 字符串翻转程序
可以使用指针来实现字符串翻转,具体代码如下:
```c
#include <stdio.h>
#include <string.h>
void reverse(char *str) {
int len = strlen(str);
char *p1 = str;
char *p2 = str + len - 1;
while (p1 < p2) {
char tmp = *p1;
*p1++ = *p2;
*p2-- = tmp;
}
}
int main() {
char str[] = "hello world";
printf("Before reverse: %s\n", str);
reverse(str);
printf("After reverse: %s\n", str);
return 0;
}
```
输出结果为:
```
Before reverse: hello world
After reverse: dlrow olleh
```
阅读全文