去掉字符串中的空格
时间:2010-03-29 来源:wangchenxicool
#include <string.h>
#include <stdio.h> //----------------------------------------------------------------------------------
void *str_l_trim( char *s )
//----------------------------------------------------------------------------------
// 去掉字符串左空格
{
int p = 0, k = 0; if( strlen(s) == 0 )
return 0;
p = 0;
while ( s[p] == ' ' || s[p] == '\t' )
{
p++;
}
if( p == 0 )
return 0;
while( s[k] != '\0')
{
s[k++] = s[p++];
} return 0;
} //----------------------------------------------------------------------------------
void *str_r_trim( char *s )
//----------------------------------------------------------------------------------
// 去掉字符串右空格
{
int l = 0, p = 0; l = strlen(s);
if( l == 0 )
return 0;
p = l - 1;
while ( s[p] == ' ' || s[p] == '\t' )
{
s[p--] = '\0';
if( p < 0 )
break;
} return 0;
} //----------------------------------------------------------------------------------
int main(void)
//----------------------------------------------------------------------------------
// 主函数
{
char *str = " wangchenxicool.cublog.cn "; str_l_trim(str);
str_r_trim(str);
printf("%s", str); getch();
return 0;
}
#include <stdio.h> //----------------------------------------------------------------------------------
void *str_l_trim( char *s )
//----------------------------------------------------------------------------------
// 去掉字符串左空格
{
int p = 0, k = 0; if( strlen(s) == 0 )
return 0;
p = 0;
while ( s[p] == ' ' || s[p] == '\t' )
{
p++;
}
if( p == 0 )
return 0;
while( s[k] != '\0')
{
s[k++] = s[p++];
} return 0;
} //----------------------------------------------------------------------------------
void *str_r_trim( char *s )
//----------------------------------------------------------------------------------
// 去掉字符串右空格
{
int l = 0, p = 0; l = strlen(s);
if( l == 0 )
return 0;
p = l - 1;
while ( s[p] == ' ' || s[p] == '\t' )
{
s[p--] = '\0';
if( p < 0 )
break;
} return 0;
} //----------------------------------------------------------------------------------
int main(void)
//----------------------------------------------------------------------------------
// 主函数
{
char *str = " wangchenxicool.cublog.cn "; str_l_trim(str);
str_r_trim(str);
printf("%s", str); getch();
return 0;
}
相关阅读 更多 +