首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

Linux 上去除字符串首尾空格的函数

2012-09-17 
Linux 下去除字符串首尾空格的函数看了很多个,这个挺简单的,可是发现居然达不到想要的功能;当输入字符串为

Linux 下去除字符串首尾空格的函数
看了很多个,这个挺简单的,可是发现居然达不到想要的功能;
当输入字符串为“ a dd ”,处理后的字符串为“a”,想修改成处理后为“a dd”
思路很乱,烦请指点一二

char *rtspace(char *str) 

char *it; 
int i,j;
while( *str == ' ' && *str)++str; 
it=str; 
while( *it == ' ' && *it)++it; 
str=it; 
strrev(str);

return str; 
}

[解决办法]

C/C++ code
char *rtspace(char *str)  {  char *it=NULL;   //指针最好初始化下while( *str == ' ' && *str)++str;  it=str;  while(*str)str++; //指针定到末尾while(*(--str)==' '); //去除最后的空格*(++str)='\0';return it;} 

热点排行