Last active
November 11, 2024 11:32
-
-
Save juanfal/fe6f9f7369ff4358c80725781949a6ca to your computer and use it in GitHub Desktop.
substrneg
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
// substrneg.cpp | |
// juanfc 2024-11-11 | |
// https://gist.github.com/juanfal/fe6f9f7369ff4358c80725781949a6ca | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
void tryThis(string s, int from, int end=-1); | |
tryThis("abcdefgh", 2); | |
tryThis("abcdefgh", -2); | |
tryThis("abcdefgh", -1); | |
tryThis("abcdefgh", -3, -2); | |
tryThis("abcdefgh", 4, 4); | |
tryThis("abcdefgh", 4, -2); | |
tryThis("abcdefgh", 4, 3); | |
tryThis("abcdefgh", 40, 50); | |
return 0; | |
} | |
string substrneg(string s, int from, int end=-1) | |
{ | |
string r; | |
int len = s.length(); | |
if (from < 0) from = len + from; | |
if (end < 0) end = len + end; | |
if (end > len-1) end = len-1; | |
for (int i = from; i <= end; ++i) | |
r += s[i]; | |
return r; | |
} | |
void tryThis(string s, int from, int to) | |
{ | |
string substrneg(string s, int from, int end=-1); | |
cout << "substrneg(\"" << s << "\", " << from <<", " << to << "): \"" << substrneg(s, from, to) << '"' << endl; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment