Titus
Titus

Reputation: 244

C++ function to split strings

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?

Upvotes: 3

Views: 247

Answers (2)

Ankit Mishra
Ankit Mishra

Reputation: 590

I made some changes in this code. replaced ispace function with s[j]==',' or s[j]!=','
Check it out.

 #include<bits/stdc++.h>
 using namespace std;

 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() && s[i]==',')
        ++i;

    // find the end of next word
    string_size j = i;
    // here i changed isspace with s[j]!=','
    while (j != s.size() && 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;
 }

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;
}

Upvotes: -1

user11422223
user11422223

Reputation:

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.

Upvotes: 3

Related Questions