-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.cpp
72 lines (62 loc) · 1.33 KB
/
string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Given a string s, display the alternate words in the reverse order.
Input:
The first line of input contains integer T denoting the number of test cases. For each test case, we input a string s.
Output:
For each test case, the output is a string displaying the words at even position in reverse order.
*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
//code
int t;
cin>>t;
string s;
getline(cin,s);
while(t--)
{
string s;
getline(cin,s);
string::iterator p=s.begin(),q=s.begin();
int i=1,j=0,flag=0;
while(j<s.length())
{
if(isspace(s[j]))
{
while(isspace(s[++j]))
{ if(j==s.length()) break;
p++; q++;
}
i++;
if(i%2==0)
{
p++; q++;
flag=1;
}
else if(i!=1)
{
reverse(p,q);
p=q;
p++;
q++;
flag=0;
}
}
else
{
q++;
if(flag==0)
{
p++;
}
}
j++;
}
if(flag==1)
reverse(p,q);
cout<<s<<endl;
}
return 0;
}