c_function_note

/*
  去除多个空格,只留一个 
*/
void deblank(char string[]){
     
     if(string==NULL)
         return;
 
     int n;
     char *cp;
     char *temp;

     cp=string;
     
     while(*cp){
         n=0;
         temp=cp;
         
         //判断多个空格 
         while(*cp==' '){
            ++n;
            ++cp;   
         }
         
         if(n>1){
             strcpy(temp+1,cp);
             cp=temp+1;        //去空后重置cp 
         }
          
          ++cp;          
                  
     }
 
}

/*
  从一个字符串中提取一个子字符串
  从src数组起始位后偏移start个字符位开始,复制len个
  非NUL字符到dst数组
  返回dst中字符串的长度 
*/
int substr(char dst[],char src[],int start,int len){
 
    if(dst==NULL || src==NULL)
 	    return 0;
	
    //起始位大于字符串长度,起始位与长度小于0,返回空字符串 
    if(strlen(src)==0 || strlen(src)<start || start<0 || len<0){ 
        dst[0]='\0';
        return 0;
    }
    
    int index;
    char *cp,*temp;
    
    cp=src+start;        //指向src偏移位 
    temp=dst; 
    for(index=0;index<len && *cp!='\0';++index,++cp,++temp)
        *temp=*cp;
     
    *temp='\0';
    
    return index;    
}

/*
  返回一个整型数中位为1的个数 
*/
int count_bits(int val){
    int i;
    int count=0;
    size_t size=sizeof(val)*8;
    
    for(i=0;i<size;++i){
        if(val&(1<<i))
            ++count;
    }
    
    return count;
    
} 
/*
  查找source字符串中匹配chars字符串中任何字符的第1个字符
  返回指向source中第1个匹配所找到的位置的指针 
*/
char *find_char(char const *source,char const *chars){
     
     if(source==NULL ||chars==NULL)
         return NULL;
         
     char *p_source;
     char *p_chars;
     
     p_source=(char *)source;
     while(*p_source){
                      
         p_chars=(char *)chars;
         while(*p_chars){
             if(*p_source==*p_chars)
                 return p_source;
             
             ++p_chars;
         }
         
         ++p_source;
     }
     
     return NULL;  
}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值