Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active November 22, 2023 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanfal/78336d0ac0c735b0ab1b97ffbbb7fd86 to your computer and use it in GitHub Desktop.
Save juanfal/78336d0ac0c735b0ab1b97ffbbb7fd86 to your computer and use it in GitHub Desktop.
split and get next piece
// t11e18.nextPiece.cpp
// juanfc 2023-11-22
//
#include <iostream>
using namespace std;
string nextPiece(string s, char sep='\0');
int main()
{
cout << '"' << nextPiece("*abc *cde**fg*", '*') << '"' << endl; // -> ""
cout << '"' << nextPiece("*abc *cde**fg*") << '"' << endl; // -> "abc "
cout << '"' << nextPiece("*abc *cde**fg*") << '"' << endl; // -> "cde"
cout << '"' << nextPiece("*abc *cde**fg*") << '"' << endl; // -> ""
cout << '"' << nextPiece("*abc *cde**fg*") << '"' << endl; // -> "fg"
return 0;
}
string nextPiece(string s, char sep)
{
static int from;
static char theSep;
if (sep != '\0') {
from = 0;
theSep = sep;
}
string r;
int i = from;
while (i < s.length() and s[i] != theSep)
r += s[i++];
from = ++i;
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment