#include <stdio.h> void swap(char *a,char *b) { *a = *a + *b; *b = *a - *b; *a = *a - *b; } void Reverse(char *arr,int left,int right)//student a am i { while(left < right) { swap(&arr[left],&arr[right]);//arr[0] left++; right--; } } int main() { int left = 0; int right = 0; char arr[]="student a am i"; Reverse(arr,0,sizeof(arr)/sizeof(arr[0])-2);//student\0 while(arr[right] != '\0') { while(arr[right] != '\0'&&arr[right] != ' ') { right++; } right--; Reverse(arr,left,right); left = right+2; right = left; } printf("%s\n",arr); return 0; }
結果是: