您的位置:首页 > 其它

删除字符串右边的全部空格

2012-05-09 19:48 295 查看
#include <iostream>
using namespace std;

//删除字符串右边的全部空格

char *my_trim( char *str )
{
if (NULL == str)
{
throw;
}

char *const address = str;
const int ONE_ELEMENT = 1;
const char NULL_TERMINATED = '\0';

while (NULL_TERMINATED != *str)
{
++str;
}

while (address != str && isspace( *(str - ONE_ELEMENT) ))
{
--str;
}

*str = NULL_TERMINATED;
str = address;

return str;
}

int main(int argc,char*argv[])
{
char buf[] = "ok2002.com              ";

cout << strlen( buf ) << endl;
my_trim( buf );
cout << strlen( buf ) << endl;

system( "PAUSE" );
return EXIT_SUCCESS;
}

/*----
24
10
请按任意键继续. . .
-----------------------*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: