Thursday, January 27, 2011

C++ programming: split a string


We have a string

  red, green, brown, blue

and we want to split it by ", " into four words

  red
  green
  brown
  blue

Method 1 -- the traditional way with the C function strtok()
  #include <vector>
  #include <string>
  #include <iostream>
  #include <string.h>
  #include <stdlib.h>
 
  using namespace std;
 
  vector<string> splitString(string input, string delimiter)
  {
     vector<string> output;
     char *pch;
 
     char *str = strdup(input.c_str());
 
     pch = strtok (str, delimiter.c_str());
 
     while (pch != NULL)
     {
        output.push_back(pch);
 
        pch = strtok (NULL,  delimiter.c_str());
     }
 
     free(str);
 
     return output;
  }
 
  int main()
  {
     string str;
 
     getline(cin, str);
 
     vector<string> output = splitString(str, ", ");
 
     for (size_t i = 0; i < output.size(); i++)
     {
        cout << output[i] << endl;
     }
 
     return 0;
  }
 
Be aware that when using strtok(), the input string will be modified. So we use strdup() to create a temporary copy and free it later on.

Method 2 -- the C++ way
  #include <vector>
  #include <string>
  #include <iostream>
 
  using namespace std;
 
  vector<string> splitString(string input, string delimiter)
  {
     vector<string> output;
     size_t start = 0;
     size_t end = 0;
 
     while (start != string::npos && end != string::npos)
     {
        start = input.find_first_not_of(delimiter, end);
 
        if (start != string::npos)
        {
           end = input.find_first_of(delimiter, start);
 
           if (end != string::npos)
           {
              output.push_back(input.substr(start, end - start));
           }
           else
           {
              output.push_back(input.substr(start));
           }
        }
     }
 
     return output;
 
  }
 
Method 3 -- using Boost.
  #include <boost/algorithm/string.hpp>
 
  #include <vector>
  #include <string>
  #include <iostream>
 
  using namespace std;
  using namespace boost;
 
  vector<string> splitString(string input, string delimiter)
  {
     vector<string> output;
 
     split(output, input, is_any_of(delimiter), token_compress_on);
 
     return output;
  }

1 comment:

Alta Noble said...

Thank you for sharing this very informative article about C++ programming. Developers of such are surely grateful. Cheers!

 
Get This <