-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_tokenize.cpp
More file actions
41 lines (37 loc) · 970 Bytes
/
string_tokenize.cpp
File metadata and controls
41 lines (37 loc) · 970 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
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void tokenize(string &s,vector<string> &tokens,string delim = " ")
{
int lastpos=s.find_first_not_of(delim,0);//skipping the delim in starting and finding starting of substring
int pos=s.find_first_of(delim,lastpos);//finding first token after substring ends
//search till either you are not able to find the start of substring or end of substring
while(lastpos!=string::npos ||pos!=string::npos)
{
tokens.push_back(s.substr(lastpos,pos-lastpos));
//skip the delimiters,find start of substring
lastpos=s.find_first_not_of(delim,pos);
//find the end of string
pos=s.find_first_of(delim,lastpos);
}
//return tokens;
}
int main()
{
string y;
int n;
cin>>n;
while(n--)
{
cin>>y;
vector<string>p;
tokenize(y,p," .-,");
for(vector<string>::iterator it=p.begin();it!=p.end();++it)
{
cout<<*it<<endl;
}
p.clear();//Deleting contents of vector
}
return 0;
}