-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseWord.cpp
More file actions
50 lines (41 loc) · 998 Bytes
/
reverseWord.cpp
File metadata and controls
50 lines (41 loc) · 998 Bytes
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string reverse(string s ){
int st = 0;
int e = s.length() - 1;
while(st < e){
swap(s[st], s[e]);
st++;
e--;
}
return s;
}
int main(){
string s;
cout << "Enter your string: ";
getline(cin, s);
vector<string> words;
int start = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == ' ' || s[i]=='\0') {
words.push_back(reverse(s.substr(start, i - start)));
start = i + 1;
}
}
// words.push_back(reverse(s.substr(start)));
// reverse(words.begin(), words.end());
// string result = "";
// for (int i = 0; i < words.size(); i++) {
// result += words[i] + " ";
// }
// result.pop_back();
cout << "The reversed string is:" << endl;
for(int i = 0;i<words.size();i++){
// cout<<a;
cout << words[i]<<" " ;
}
return 0;
}