-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path#37 (3) - Split String.txt
More file actions
56 lines (46 loc) · 905 Bytes
/
Copy path#37 (3) - Split String.txt
File metadata and controls
56 lines (46 loc) · 905 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
51
52
53
54
55
56
#include<iostream>
#include<string>
#include<vector>
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;
}
void PrintVector(vector <string> &vString)
{
for (const string &Word : vString)
{
cout << Word << endl;
}
cout << endl;
}
int main()
{
vector <string> vString= SplitString(ReadString(), " ");
cout << "\nTokens = " << vString.size() << endl;
PrintVector(vString);
return 0;
}