-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path#41 (3) - Reverse Words.txt
More file actions
66 lines (51 loc) · 1.03 KB
/
Copy path#41 (3) - Reverse Words.txt
File metadata and controls
66 lines (51 loc) · 1.03 KB
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
#include<iostream>
#include<vector>
#include<string>
using namespace std;
string ReadString()
{
string Word;
cout << "Please enter yor string ?" << endl;
getline(cin, Word);
return Word;
}
vector <string> SplitString(string S1, string delim)
{
short pos = 0;
string sWord = "";
vector <string> vString;
while ((pos = S1.find(delim)) != std::string::npos)
{
sWord = S1.substr(0, pos);
if (sWord != "")
{
vString.push_back(sWord);
}
S1.erase(0, pos + delim.length());
}
if (S1 != "")
{
vString.push_back(S1);
}
return vString;
}
string ReverseWordsInString(string S1)
{
vector <string> vString = SplitString(S1, " ");
string S2 = "";
vector <string>::iterator iter = vString.end();
while (iter != vString.begin())
{
--iter;
S2 += *iter + " ";
}
S2 = S2.substr(0, S2.length() - 1);
return S2;
}
int main()
{
string S1 = ReadString();
cout << "\n\nString after Reversing words :\n";
cout << ReverseWordsInString(S1) << endl;
return 0;
}