/*
去除多个空格,只留一个
*/
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;
}