/*
?* 将"I am? a?? programmer"输出成"programmer?? a? am I"
?* 注意:相邻两个单词的空格数一次是1个,2个,3个
?*/
#include<stdio.h>
#include<string.h>
/*
?* 反转字符串
?*/
void reverse(char *begin,char *end)
{
?while(begin<end)
?{
??char temp=*begin;
??*begin=*end;
??*end=temp;
??begin++;
??end--;
?}
}
void reverseWords(char *str)
{
?char *s1=str;
?char *s2=str;
?char *s3=str;
?while(*s3!='\0')s3++;
?s3--;
?while(s2<=s3)
?{
??if(*s2!=' ')
??{
???if(*(s2-1)==' ')
???{
????s1=s2;
???}
???s2++;
??}
??//s2指向空格,s2-1指向非空格
??else if(*s2==' '&&*(s2-1)!=' ')
??{
???reverse(s1,s2-1);
???s2++;
??}
??//s2指向空格,s2-1指向空格
??else
??{
???s2++;
??}
?}?
}
int main()
{
?char s[]="I am? a?? programmer";
?reverse(s,s+strlen(s)-1);
?reverseWords(s);
?printf("%s\n",s);
?return 0;
}