Last active
November 22, 2023 19:45
-
-
Save juanfal/78336d0ac0c735b0ab1b97ffbbb7fd86 to your computer and use it in GitHub Desktop.
split and get next piece
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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