题解 | #倒置字符串#
倒置字符串
https://www.nowcoder.com/practice/ee5de2e7c45a46a090c1ced2fdc62355
C语言指针的经典解法!! #include <stdio.h> #include <string.h> void sort(char* star, char* end) { while (star < end) { char p = *star; *star = *end; *end = p; star++; end--; } } int main() { char a[100]; char b[100]; int i; gets(a); int len = strlen(a); char* star = a; sort(a, a + len - 1); for (i = 0; i <= len; i++) { if (*(a + i) == ' ' || *(a + i) == '\0') { sort(star, a + i - 1); star = (a + i + 1); } } printf("%s", a); return 0; }