经典代码三

来源:互联网 发布:软件需求确认书模板 编辑:程序博客网 时间:2024/05/01 21:45

1、函数squeeze(s,c),它删除字符串s中出现的字符c:

void squeeze(char s[] , int c){    int i;    int j;    for( i = j = 0; s[i] != '\0' ; i ++ )    {if( c != s[i] ){    s[j ++] = s[i];}    }    s[j] = '\0';    }

2、strcat函数:将字符串t连接到字符串s的尾部,s必须有足够大的空间

void strcat(char s[], char t[]){    int i, j;    i = j = 0;    while( '\0' != s[i] )    {i ++;    }    while( '\0' != (s[i ++] = t[j ++]) )    {;    }}

3、getbits(x, p, n),返回x中从右边数第p位开始向右数n位的字段。这里假定最右边的一位是第0位,n与p都是合理的正值。如:getbits(x,4,3)返回x中4,3,2三位的值。

unsigned getbits(unsigned x, int p, int n){    return (x >> (p+1-n)) & ~(~0 << n);}