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;
}
Podarce
  • 473
  • 1
  • 6
  • 22

1 Answers1

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)).

Podarce
  • 473
  • 1
  • 6
  • 22