以i from shanghai为例 输出 shanghai from i
#include <stdio.h>
#define MAX_SIZE 1024
void reverse_string(char *src, int len)
{
int i;
char temp;
for(i = 0; i < len / 2; i++)
{
temp = *(src + i);
*(src + i) = *(src + len - 1 - i);
*(src + len - 1 - i) = temp;
}
}
void reverse_sentence(char *src)
{
int len = 0;
while(*src != '\0')
{
if(*src == ' ')
{
reverse_string(src - len, len);
len = 0;
}
else
{
len++;
}
src++;
}
reverse_string(src - len, len);
}
int main()
{
char src[MAX_SIZE];
printf("Please input sentence:\n");
gets(src);
reverse_string(src,strlen(src));
printf("src = %s\n",src);
reverse_sentence(src);
printf("src = %s\n",src);
return 0;
}