题解 | #倒置字符串#
倒置字符串
https://www.nowcoder.com/practice/ee5de2e7c45a46a090c1ced2fdc62355
#include <stdio.h> #include<assert.h> #include<string.h> void reverse(char* left,char* right) { assert(left&&right); while(left<right) { char ret=*left; *left=*right; *right=ret; left++; right--; } } int main() { char arr[101]={0}; gets(arr); int len=strlen(arr); reverse(arr,arr+len-1); char *str=arr; while(*str) { char* start=str; while(*str!=' '&&*str!='\0') { str++; } reverse(start,str-1); if(*str!='\0') str++; } printf("%s\n",arr); return 0; }