HDU_1062Text Reverse

来源:互联网 发布:医院没有编制 知乎 编辑:程序博客网 时间:2024/05/08 06:53
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.
 

Input
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.
 

Output
For each test case, you should output the text which is processed.
 

Sample Input
3olleh !dlrowm'I morf .udhI ekil .mca
 

Sample Output
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.
 代码:
#include<iostream>#include <stdio.h>  #include <string.h>  using namespace std;  int main()  {      int n,i,j,m,a,b;      char str[1000],s[1000][1000],z;        cin>>n;     getchar();      while(n--)      {          gets(str);          memset(s,'\0',sizeof(s)); //s[]初始化为‘\0’,字符串的结束符         a = b = 0;          m = strlen(str);          for(i = 0; i < m;i++)          {              if(str[i] == ' ')              {                  a++;  //a计数空格                b = 0; //每次遇到空格都更新为“0”.                 continue;              }              s[a][b] = str[i];//用s[a][b]数组来存放每个空格隔断的字符串              b++;  //计数空格后的字符        }          for(i = 0;i <= a;i++)          {              int w = strlen(s[i]);  //每个空格后的字符串            for(j = 0;j<w/2;j++)//字符串的反转              {                  z = s[i][j];                  s[i][j] = s[i][w-j-1];                  s[i][w-j-1] = z;              }              printf("%s",s[i]);              if(i!=a)//输出空格              printf(" ");          }          printf("\n");      }        return 0;  } 

思路解析:每个空格后的字符反转
本题只有空格这一个特殊字符,以他为标记,记录单词。切记空格的输出要判断再输出。。。



0 0
原创粉丝点击