#include <stdio.h>
#include <string.h>
void copy(char *words, char *p, int length);
void main()
{
char *sentence;
char *reverseSentence;
char *ptr;
int position[100];
int number = 0;
//空格数
int i=0, j=0;
int currentLength = 0;
int wordLength=0;
sentence = "my name is xiao hua";
ptr = sentence;
position[0] = 0;
while(*ptr != '\0')
{
if(*ptr++ == ' ')
{
position[number+1] = (ptr-1)-sentence;
number++;
}
}
ptr = ptr - 1;
for(i=0,j=number; i<=number,j>=0; i++,j--)
{
if(j ==0 )
{
wordLength = ptr-(sentence+position[j]) + 1;
copy(reverseSentence+currentLength, sentence+position[j], wordLength);
}
else
{
wordLength = ptr-(sentence+position[j]);
copy(reverseSentence+currentLength, sentence+position[j]+1, wordLength);
}
currentLength += wordLength+1;
ptr = sentence + position[j]-1;
}
printf("%s", reverseSentence);
}
void copy(char *words, char *p, int length)
{
int count = 0;
while(count < length)
{
*words = *p;
words = words +1;
p = p+1;
count++;
}
*words = ' ';
}