Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ function to split strings

Tags:

c++

I have recently started learning C++ using the book Accelerated C++. The book introduces the following function to split a string up into substrings based on whitespace characters

vector<string> split(const string& s)
{
    vector<string> ret;
    typedef string::size_type string_size;
    string_size i = 0;

    while (i != s.size()) {
        // ignore leading blanks
        // invariant: characters in range[original i, current i) are all spaces
        while (i != s.size() && isspace(s[i]))
            ++i;

        // find the end of next word
        string_size j = i;
        // invariant: none of the characters in range [original j, current j) is a space
        while (j != s.size() && !isspace(s[j]))
            j++;

        if (i != j) {
            // copy from s starting at i and taking j-i chars
            ret.push_back(s.substr(i, j - i));
            i = j;
        }
    }
    return ret;
}

My goal is to use this function to split strings based on commas. So I changed the !isspace(s[j]) part into s[j] != ',' so that the function uses commas to recognise the end of a word. I tried to test the function as follows:

int main() {

    string test_string = "this is ,a test ";

    vector<string> test = split(test_string);

    for (vector<string>::const_iterator it = test.begin(); it != test.end(); ++it)
        cout << *it << endl;

    return 0;
}

When I compile the function in my terminal as follows

g++ -o test_split_function test_split_function.cpp

I get no errors. When I run the script however, it keeps running and doesn't produce any output in my command line. I don't understand why, since the function that split the string up into words based on whitespace did run on the same string.

Question: What am I doing wrong? Is the s[j] != ',' statement incorrect?

like image 335
Titus Avatar asked Sep 11 '26 15:09

Titus


1 Answers

There are two parts where you need to replace the isspace() function, one for the i loop:

while (i != s.size() && s[i]==',')
   ++i;

one for the j loop:

while (j != s.size() && s[j]!=',')
   j++;

This will work.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!