-
Notifications
You must be signed in to change notification settings - Fork 0
/
strrev.cpp
54 lines (46 loc) · 989 Bytes
/
strrev.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
//Reversing words of a string
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mk make_pair
#define f first
#define se second
#define ll long long int
#define ld long double
#define MOD 1000000007
int main()
{
string s;
getline(cin,s);
//cout<<s<<endl;
int n=s.size(); //cout<<n<<endl;
string ans; string temp;
for(int i=n-1;i>=0;i--)
{
if(temp.size()==0&&s[i]==' ')
{
continue;
}
else if(s[i]==' ')
{
//cout<<i<<endl;
reverse(temp.begin(),temp.end());
temp.pb(s[i]);
//cout<<temp.back()<<endl;
ans.append(temp);
temp.clear();
}
else
temp.pb(s[i]);
}
if(temp.size())
{
reverse(temp.begin(),temp.end());
ans.append(temp);
}
if(ans.size()&&ans.back()==' ')
ans.pop_back();
cout<<ans<<endl;
//cout<<ans.size()<<endl;
return 0;
}