题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062
题目描述:
Problem Description
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
3 olleh !dlrow m'I morf .udh I ekil .mca
Sample Output
hello world! I'm from hdu. I like acm.
题目分析:
题目本身并不是什么难的题目,但是我在这里写的原因是觉得字符串处理真的非常重要,所以还是多写几遍比较好
AC代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
char s[1005];
int main()
{
cin>>n;
getchar();
while(n--)
{
gets(s);
int l=strlen(s);
for(int i=0;i<l;)
{
if(s[i]==' ')
{
printf("%c",s[i]);
i++;
continue;
}
while(s[i]!=' '&&i<l)
i++;
for(int j=i-1;s[j]!=' '&&j>=0;j--)
{
printf("%c",s[j]);
}
}
printf("\n");
}
return 0;
}