Anyone got a good way to do this without having to use a c-string (and <cstring>):
Code:#include <iostream> #include <string> #include <cstring> using namespace std; void stripWS(string &str) { int len = str.size(); char copy[len+1], *p; strcpy(copy,str.c_str()); p = ©[len-1]; while (*p == ' ' || *p == '\t') *(p--) = '\0'; p = copy; while (*p == ' ' || *p == '\t') *p++; str = p; } int main() { string test("\t\t so what\t "); cout << "->" << test << "<-\n"; stripWS(test); cout << "->" << test << "<-\n"; return 0; }



LinkBack URL
About LinkBacks


