我能够反转一个字符串。例如,我可以将“reverse a string”反转为“esrever a gnirts”。但我无法像“string a reverse”那样逐字颠倒它。
void reverseString(char string[],char *start, char* end)
{
char tmp; //temporary variable to swap values
int count = 0;
while(start<end)
{
if(*start==' ')
{
printf("found space count %d \n",count);
reverseString(string,start-count,start);
}
tmp = *start;
*start = *end;
*end = tmp;
*start++;
*end--;
count++;
}
printf(" string %s \n", string);
}
int main()
{
char string[] = "reverse a string word by word";
char *start =string;
char *end =start+ strlen(string) -1;
reverseString(string,start,end);
return 0;
}