Word Scramble |
Write a program that will reverse the letters in each of a sequence of words while preserving the order of the words themselves.
Input
The input file will consist of several lines of several words. Words are contiguous stretches of printable characters delimited by white space.
Output
The output will consist of the same lines and words as the input file. However, the letters within each word must be reversed.
Sample Input
I love you. You love me. We're a happy family.
Sample Output
I evol .uoy uoY evol .em er'eW a yppah .ylimaf
#include"stdio.h" int main() { char str[30000]; int a,i,m,n,k,temp; while(gets(str)) { m=0; i=0; for(;str[i]!='\0';i++) { if(str[i]==' '&&m==0)//为使第一个词在前面没有空格的情况下,后面有空格也可以扭转; { m=i+1;k=i-1; for(a=0;a<k;a++) {temp=str[a];str[a]=str[k];str[k]=temp;k--;} } else if(str[i]==' '&&m!=0)//在前面已经出现过空格的情况下,对再出现空格之前的词进行扭转; { n=i-1; k=i; while(str[k]==' ') k++; for(;m<n;m++) {temp=str[m];str[m]=str[n];str[n]=temp;n--;} m=k; } if(str[i+1]=='\0')//对结束的词也就行扭转; {n=i; for(;m<n;m++) {temp=str[m];str[m]=str[n];str[n]=temp;n--;} } } printf("%s\n",str); } return 0; }