您的位置:首页 > 其它

zoj 1151 Word Reversal(字符串操作模拟)

2018-03-11 11:38 453 查看
题目连接:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1151

题目描述:

For each list of words, output a line with each word reversed without changing the order of the words.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed
by N input blocks. Each input block is in the format indicated in the problem
description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between
output blocks.

Input

You will be given a number of test cases. The first line contains a positive
integer indicating the number of cases to follow. Each case is given on a line
containing a list of words separated by one space, and each word contains only
uppercase and lowercase letters.

Output

For each test case, print the output on one line.

Sample Input

1

3

I am happy today

To be or not to be

I want to win the practice contest

Sample Output

I ma yppah yadot

oT eb ro ton ot eb

I tnaw ot niw eht ecitcarp tsetnoc

1 /*问题 将一段文字的每个单词反转,但次序不变
2 解题思路 模拟,具体算法见注释处的坑点*/
3 #include<cstdio>
4 #include<iostream>
5 #include<cstring>
6 #include<cctype>
7 #include<string>
8 #include<algorithm>
9 using namespace std;
10
11 int main()
12 {
13     int T,t;
14     char str[101],str1[101];
15     string s;
16     scanf("%d",&T);
17     while(T--){
18         scanf("%d",&t);
19         getchar();
20         while(t--){
21             cin.getline(str,101);
22
23             int len=strlen(str);
24             int i;
25             for(i=len-1;i>=0;i--)
26                 if(isalpha(str[i])) break;
27             str[i+1]=' ';
28             str[i+2]='\0';
29
30             char *p=NULL;
31             p=str;
32
33             while(*p == ' ')
34                 p++;
35
36             len=strlen(p);
37             for(i=0;i<len;i++){
38                 if(p[i]!=' ')
39                 {
40                     s += p[i];
41                 }
42                 else
43                 {
44                     reverse(s.begin(),s.end());
45                     cout<<s;
46                     if(i != len-1) printf(" ");//坑点1
47                     s="";
48                 }
49             }
50             cout<<endl;
51             s="";
52         }
53         if(T != 0) printf("\n");//坑点2
54     }
55     return 0;
56 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: