您的位置:首页 > 其它

hdoj 练习题1.2.2 字符串反转

2015-11-09 19:59 274 查看
[align=left]Problem Description[/align]
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

[align=left]Input[/align]
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.

Each test case contains a single line with several words. There will be at most 1000 characters in a line.

[align=left]Output[/align]
For each test case, you should output the text which is processed.

[align=left]Sample Input[/align]

3
olleh !dlrow
m'I morf .udh
I ekil .mca


[align=left]Sample Output[/align]

hello world!
I'm from hdu.
I like acm.
Hint
Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.

[align=left]Author[/align]
Ignatius.L

 
 
 
 
解题思路:
注意:用getchar()消去录入的换行,用gets()函数直接录入字符串
 
 
录入字符串之后设置两个变量uper 和 lower ,以空格为单位反转输出字符串
 
 
源代码:
 
# include <stdio.h>

# include <string.h>

char s[1001];

int main()

{

 int n;

 while (scanf("%d",&n)!=EOF)

 {

  getchar();//接收=0,传过来的空行

  while (n--)

  {

   gets(s);

   int lower=0,uper=0;

   int i=0,j=0;

   int len=strlen(s);

   for (i=0;i<len;i++)

   {

    while ((s[i]!=' ')&&(s[i]!='\0'))

     i++;

    uper=i-1;

    for (j=uper;j>=lower;j--)

     printf("%c",s[j]);

    lower=uper+2;

    if (s[i]==' ')

     printf(" ");

   }

   printf("\n");

  }

 

 }

  return 0;

}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: