2

I'm writing my code in C++98 using only standard libraries. I'm trying to write some code to split a string in multiple substrings each delimited by the string "OK" or the string "ERROR". Each substring should be put in the mysubstring array.

This is the code I wrote for a single separator:

void split_string()
{
   for (unsigned short k=0;k<10;k++)
   {
      mysubstring[k]=""; //resetting all substrings
   }
   string separator = "OK";
   size_t pos = 0;
   unsigned short index=0;
   while ((pos = str_to_split.find(separator)) != std::string::npos) {
   mysubstring[index] = str_to_split.substr(0, pos);
   str_to_split.erase(0, pos + separator.length());
   index++;
}

This single separator version works fine. Then I tried to upgrade to the two separators:

    void split_string()
    {
        for (unsigned short k=0;k<10;k++)
        {
            mysubstring[k]=""; //resetting all substrings
        }
        string okseparator = "OK";
        string koseparator = "ERROR";
        size_t  okpos = 0;
        size_t  kopos = 0;
        unsigned short index=0;
        while ((okpos = string_to_split.find(okseparator)) != std::string::npos)
       {
        while ((kopos = string_to_split.find(koseparator)) != std::string::npos)
          {
            if (okpos <= kopos)
            {
               mysubtring[index] = string_to_split.substr(0, okpos + okseparator.length());

               string_to_split.erase(0, okpos + okseparator.length());
               index++;
            }
            else
            {
                mysubstring[index] = string_to_split.substr(0, kopos + koseparator.length());
                string_to_split.erase(0, kopos + koseparator.length());
                index++;
            }
        }
    }

        while ((kopos = string_to_split.find(koseparator)) != std::string::npos)
        {
            mysubtring[index] = string_to_split.substr(0, kopos + koseparator.length());
            string_to_split.erase(0, kopos + koseparator.length());
            index++;
            }
}

The idea here is that I stay inside the first while loop untill all "OK" are consumed, then it enters the last while to finish off all "ERROR" left. The substrings should enter the mysubstring array in the same order they are in the string_to_split original string.

Sadly I can't get it to work, could you help me ?

Example to test and verify:

#include <iostream>
#include <string.h>

void split_string();

string str_to_split = "skdjfnsdjknfjk OK    fkjsnfjksdnfjnsdjkfn ERROR skjdfnjksdnf OK sjkdnfjksdnfjERROR jnfjnsdjfnsjdknfjkn       OK";

use namespace std;
int main()
{
  split_string();
  return 0;
}
4
  • Why doesn't this work? What exactly do you want it to do, and what is the actual result you are seeing?
    – MPops
    Jun 12, 2018 at 18:40
  • I see an infinite loop, I guess when okpos = kopos in particular when okpos = max(size_t) = kopos. This happens when string::find == npos
    – Podarce
    Jun 12, 2018 at 18:41
  • How about posting a minimal reproducible example that we can actually test, rather than just code snippets? Jun 12, 2018 at 18:51
  • Sure, sorry. Added.
    – Podarce
    Jun 12, 2018 at 19:09

1 Answer 1

0

Figured it out:

    void split_string()
    {
        for (unsigned short k=0;k<10;k++)
        {
            mysubstring[k]=""; //resetting all substrings
        }

    string okseparator = "OK";
    string koseparator = "ERROR";
    size_t okpos = 0;
    size_t kopos = 0;
    unsigned short  index=0;

    while (1)
        {
        okpos = string_to_split.find(okseparator);
        kopos = string_to_split.find(koseparator);
        if (okpos < kopos)
        {
            mysubstring[index] = string_to_split.substr(0, okpos + okseparator.length());
            string_to_split.erase(0, okpos + okseparator.length());
            index++;
        }
        else if (okpos > kopos)
        {
            mysubstring[index] = string_to_split.substr(0, kopos);
            string_to_split.erase(0, kopos + koseparator.length());
            index++;
        }
        else
        {
            break;
        }
        }
}

I get the position for both separators but I consider only the closest one. The while(1) terminates when both the separators have the same position (string::npos = max(size_t)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.