Thanks John,
your suggestion does solve the white space problem. But i do have
to enter twice. I don't know why? using the exact above code i do have
to enter twice.
let me just write it
enter some string: John Ratliff
12
so it does need two returns
i am again coping the code below:
thought of attaching screenshot but can't see any attachment tab out
here.
#include <iostream>
#include <string>
#include "str.hpp"
using namespace std;
int main(int argc, char* argv[]) {
std::string s;
std::string::size_type len;
std::cout << "enter some string: ";
std::getline(std::cin, s);
trim(s);
len = s.length();
std::cout << len << std::endl;
cout << s;
return 0;
}
static void trim(std::string &str) {
std::string::size_type pos = str.find_last_not_of(' ');
if (pos != std::string::npos) {
str.erase(pos + 1);
pos = str.find_first_not_of(' ');
if (pos != std::string::npos) {
str.erase(0, pos);
}
} else {
str.erase(str.begin(), str.end());
}
}
void trim2(string& str)
{
string::size_type pos = str.find_last_not_of(' ');
if(pos != string::npos) {
str.erase(pos + 1);
pos = str.find_first_not_of(' ');
if(pos != string::npos) str.erase(0, pos);
}
else str.erase(str.begin(), str.end());
}