[Please do not mail me a copy of your followup]
"Thomas J. Gritzan" <
[email protected]> spake the secret code
Am 18.01.2010 16:10, schrieb Richard:
No. It uses find_last_not_of(whitespaces) to find the last character
that is not whitespace and removes the rest.
Yes, I noticed that after I hit send
.
s/first/last/
Still, that's not what chomp does. Chomp removes trailing whitespace,
not everything after the last whitespace. A proper implementation of
chomp would be:
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
string chomp(string const &text)
{
string result = text;
string const whitespaces(" \t\f\v\n\r");
size_t const found = result.find_last_not_of(whitespaces);
if (result.length() > 0)
{
while (whitespaces.find(result[result.length() - 1]) != string::npos)
{
result.erase(result.length() - 1);
}
}
return result;
}
int main()
{
string const input("My name is Larry \n\rother stuff\r\n");
string const expected("My name is Larry \n\rother stuff");
assert(expected == chomp(input));
cout << chomp(input);
return 0;
}