#include <iostream>
#include <string>
using namespace std;
string trim(string str);
int main()
{
string name(" wei ");
cout << name<<endl;
name = trim(name);
cout << name<<endl;
return 1;
}
string trim(string str)
{
int len = (int)str.length();
//分配空间+赋值
char *tempstr = new char[len+1];
memset(tempstr, 0, len+1);
memcpy(tempstr, str.c_str(), len);
char *src = tempstr;
去掉字符串前面的空格
while ( *src != '\0')
{
if ( *src ==' ' || *src == '\t' )
{
src++;
}
else
{
break;
}
}
int count = strlen(src);
int i = count;
//去掉字符串后面的空格
for (int j= count-1; j> 0; j-- )
{
if ( src[j] != '\t' && src[j] != ' ')
{
break;
}else
{
i = j;
}
}
if( src[i] !='\0')
{
src[i] = '\0';
}
string s(src); //string 构造函数
}
#include <string>
using namespace std;
string trim(string str);
int main()
{
string name(" wei ");
cout << name<<endl;
name = trim(name);
cout << name<<endl;
return 1;
}
string trim(string str)
{
int len = (int)str.length();
//分配空间+赋值
char *tempstr = new char[len+1];
memset(tempstr, 0, len+1);
memcpy(tempstr, str.c_str(), len);
char *src = tempstr;
去掉字符串前面的空格
while ( *src != '\0')
{
if ( *src ==' ' || *src == '\t' )
{
src++;
}
else
{
break;
}
}
int count = strlen(src);
int i = count;
//去掉字符串后面的空格
for (int j= count-1; j> 0; j-- )
{
if ( src[j] != '\t' && src[j] != ' ')
{
break;
}else
{
i = j;
}
}
if( src[i] !='\0')
{
src[i] = '\0';
}
string s(src); //string 构造函数
delete tempstr;
tempstr = NULL;
return s;}