几个自己写的字符串C函数

自己写的字符串去除空格C函数


1) 在字符串s中剔出指定的字符c

static char* trim(char *s, char c)

{
return (*s==0)?s:(((*s!=c)?(((trim(s+1,c)-1)==s)?s:(*(trim(s+1,c)-1)=*s,*s=c,trim(s+1,c))):trim(s+1,c)));

}

例子:

char str[] = " M y B eautiful C hin a ! ";

char *s = trim(str, 32);

+ s 0x0030fd0f "MyBeautifulChina!" char *


2) 在字符串s中剔出左侧的指定字符c

static char* ltrim(char *s, char c)
{
while(*s!=0&&*s==c){s++;}return s;
}

例子:

char str[] = " M y B eautiful C hin a ! ";

char *s = ltrim(str, 32);

+ s 0x0028f804 "M y B eautiful C hin a ! " char *


2) 在字符串s中剔出右侧的指定字符c

static char* rtrim(char *s, char c)
{
char *p=s,*q=s;while(*p!=0){if(*p!=c){q=p;q++;}p++;}if(q!=s)*q=0;return s;

}

例子:

char str[] = " M y B eautiful C hin a ! ";

char *s = rtrim(str, 32);

+ s 0x002cf6fc " M y B eautiful C hin a !" char *


char *s = rtrim(ltrim(str, 32), 32);

+ s 0x0031fa78 "M y B eautiful C hin a !" char *



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值