Advertisement
istinishat

string tokenization

Feb 3rd, 2019
1,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://www.geeksforgeeks.org/tokenizing-a-string-cpp/
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. string str,x;
  6. vector<string>tokens;
  7.  
  8. int string_to_int(string now)
  9. {
  10.     int ret=0,i;
  11.     for(i=0;i<now.size();i++){
  12.         ret=(ret*10+(now[i]-'0'));
  13.     }
  14.     return ret;
  15. }
  16.  
  17.  
  18. int main()
  19. {
  20.     freopen("input.txt","r",stdin);
  21.    
  22.     ios_base::sync_with_stdio(false);
  23.     cin.tie(NULL);
  24.  
  25.     int i,j;
  26.     cin>>str;
  27.     stringstream ss(str);
  28.     while(getline(ss,x,',')){
  29.         tokens.push_back(x);
  30.     }
  31.  
  32.     for(auto xx : tokens)cout<<xx<<endl;
  33.  
  34.     cout<<"============"<<endl;
  35.  
  36.     for(i=1;i<tokens.size();i+=2){
  37.         int now=string_to_int(tokens[i]);
  38.         cout<<now<<endl;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement