#include <iostream>
#include <vector>
#include <string>
using namespace std;
string& trim(string &str);
string& ltrim(string &str);
string& rtrim(string &str);
int main(int argc, char *argv[])
{
string str1 = " Ni Hao ";
string str2 = " Bonjour ";
string str3 = " Hello ";
cout << "[" <<str1<< "]" <<endl;
cout << "[" <<trim(str1)<< "]" <<endl;
cout << "[" <<str2<< "]" <<endl;
cout << "[" <<ltrim(str2)<< "]" <<endl;
cout << "[" <<str3<< "]" <<endl;
cout << "[" <<rtrim(str3)<< "]" <<endl;
}
string& trim(string &str)
{
int i,j,start,end;
//ltrim
for (i=0; (str[i]!=0 && str[i]<=32); )
i++;
start=i;
//rtrim
for(i=0,j=0; str[i]!=0; i++)
j = ((str[i]<=32)? j+1 : 0);
end=i-j;
str = str.substr(start,end-start);
return str;
}
string& ltrim(string &str)
{
int i,start;
for (i=0; (str[i]!=0 && str[i]<=32); )
i++;
start=i;
str = str.substr(start,str.length()-start);
return str;
}
string& rtrim(string &str)
{
int i,j,end;
for(i=0,j=0; str[i]!=0; i++)
j = ((str[i]<=32)? j+1 : 0);
end=i-j;
str = str.substr(0,end);
return str;
}