将原来的字符串逆序存放abcdefgh
时间: 2024-02-11 07:06:40 浏览: 69
如何将字符串逆序.rar
可以使用双指针法,从字符串的两端开始向中间遍历,交换两个指针所指的字符,直到遍历到中间位置。具体实现如下:
```
#include <stdio.h>
#include <string.h>
void reverse_string(char *s) {
int len = strlen(s);
int i = 0, j = len - 1;
while (i < j) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
int main() {
char s[] = "abcdefgh";
printf("original string: %s\n", s);
reverse_string(s);
printf("reversed string: %s\n", s);
return 0;
}
```
运行结果为:
```
original string: abcdefgh
reversed string: hgfedcba
```
阅读全文