c常用字符实战操作

 

//清空left空字符

static char * trim_left(char *buf)
{

int len,i;
 char * tmp = NULL;

 len = strlen(buf);
 if(len==0)
 {
  return buf;
 }
 tmp = (char*)malloc(len);
 memset(tmp,0x00,len);
 for(i = 0;i < len;i++)
 {
  if (buf[i] !=' ')
  {
   break;
  }
 }
 if (i < len)
 {
  strncpy(tmp,(buf+i),(len-i));  /*i is the number of space in the left*/
 }
 strncpy(buf,tmp,len);
 free(tmp);
 return buf;

}

 

//清空right空字符


static char * trim_right(char *buf)
{
 int len,i;   
 char * tmp=NULL;
 
 len = strlen(buf);
 if(len==0)
 {
  return buf;
 }
 tmp = (char*)malloc(len);
 memset(tmp,0x00,len);
 for(i = 0;i < len;i++)
 {
  if (buf[len-i-1] !=' ')
  {
   break;
  }
 }
 if (i < len)
 {
  strncpy(tmp,buf,len-i);
 }
 strncpy(buf,tmp,len);
 free(tmp);
 return buf;} 

//清空空字符

static int getstr_trim(char *outstr, char *instr)
{
 if ((NULL == outstr) || (NULL == instr))
 {
  return NULL;
 }
 int i = 0;
 char *tmp = instr;
 while(*instr != ' ')
 {
  i++;
  instr++;
 }
 memcpy(outstr, tmp, i);
 return 0;
}

 

//清空指定字符

static int getstr_trimchar(char *instr, char trim_char)
{
 char tmp_str[50];
 if (NULL == instr)
 {
  return D_FAILURE;
 }
 if (strlen(instr) > sizeof(tmp_str))
 {
  return D_FAILURE;
 }
 int i = 0;
 char *tmp = tmp_str;
 strcpy(tmp_str,instr);
 
 while(*instr)
 {
  if(*tmp != trim_char)
  {
   *instr = *tmp;
   instr++;
  }
  tmp++;
 }
 *instr = '\0';
 return D_SUCCESS;

}

转载于:https://www.cnblogs.com/fx2008/archive/2011/10/14/2211993.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值