博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
常用函数编写
阅读量:5346 次
发布时间:2019-06-15

本文共 8629 字,大约阅读时间需要 28 分钟。

注意:最好的代码格式时候,我们还可以使用断言在函数的开始处检查参数的有效性。

strlen、strcpy、strcat、strcmp

int stringlen(const char *s){    int i = 0;    assert(s != NULL);    while (s[i])    {        i++;    }    return i;}
char *stringcpy(char *s1, const char *s2){    char *p = s1;        assert((NULL != s1) && (NULL != s2));    while((*s1++ = *s2++) != '\0');        return p;}
char *stringcat(char *s1, const char *s2){    char *p = s1;        assert((NULL != s1) && (NULL != s2));    while(*s1)    {        s1++;    }    while(*s1++ = *s2++);        return p;}
int stringcmp(char *s1,char *s2){    assert((NULL != s1) && (NULL != s2));    while((*s1 == *s2)&& *s1)    {        s1++;        s2++;    }    return (*s1 -*s2);}

strchr、strrchr

char *stringchr(const char *s, char c){    assert(NULL != s);    while(*s)    {        if(*s == c)        {            return s;        }        s++;    }        return NULL;}
char *stringrchr(const char *s,char c){    char *p = NULL;        while(*s)    {        if(*s == c)        {            p = s;        }        s++;    }        return p;}
char *myitoa(int num,char *s){         int I = 0;    while(num){                   s[i++] = num%10 +’0’;                   num /= 10;    }    S[i] = 0;    Reverse(s);    Return s;}

countchar

int countchar(const char *s,char c){    int num = 0;        assert(NULL != s);    while(*s)    {        if(*s == c)        {            num++;        }        s++;    }        return num;}

max、secondmax

int max(int arr[],int n){    int i = 0;    int max = arr[0];        for(i=1; i
int find_secondmax(int arr[], int n){         int I;    int max = arr[o];         int sec = arr[1];         if(arr[0] 
max){ Sec = max; Max = arr[i]; } If((arr[i] <=max) && (arr[i] >second)) Sec = arr[i]; } Return sec;}

reverse

char *reverse(char *s);char *reverse(char *s){         //interative         Inttemp, I, j = strlen(s)-1;         For(I = 0;i<=j; j--, i++){                   Temp = s[i];                   S[i] = s[j];                   S[j] = temp;    }    Return s;}
Char *reverse(char *s){         //recursive         Int I = 0;         Char c;         Int n = strlen(s);         If(n == 1)                   Return;         C = s[n -1];         For(i=n-1;i>0;i--)                   S[i] = s[i-1];         S[0] = c;         Reverse(s++);}char *myitobin(int num,char *s){         int I = 0;    while(num){                   s[i++] = num%2 +’0’;                   num /= 2;    }    S[i] = 0;    Reverse(s);    Return s; }

iton、atoi、htoi

char *myiton(int num,char *s,int base){    int i = 0;             if(base >=2 && base <=10)    {        while(num)        {            s[i ++] = num % base + '0';            num /= base;        }    }    else    {        if(base >= 11 && base <= 16)        {            if(num %base >= 10)            {                s[I ++] = num % base - 10 + 'a';            }                   else            {                s[i++] = num % base + '0';            }        }        else        {            fprintf(stderr, "base if error");            return NULL;        }    }    s[i] = 0;    reverse(s);        return s;}
int myatoi(char *s){    int n = 0;        while(*s >= '0' && *s <= '9')    {        n = n*10 +(*s –'0');        s ++;    }        return n;}
int myhtoi(char *s){    int n = 0;        if((s[0] == ‘0’)&&((s[1] == ‘x’)||(*s == ‘X’)))    {        s = s +2;    }    while(*s)    {        if(*s >= '0' && *s <= '9')        {            n = n*16 +(*s –'0');        }        else if(*s >= 'a' && *s <= 'f')        {            n = n*16 +(*s –'a')+10;        }        else if(*s >= 'A' && *s <= 'F')        {            n = n*16 +(*s –'A') + 10;        }        else        {            break;        }       s++;    }        return n;}

strncmp

int stringncmp(char *s1, char *s2, int n){    while((*s1 == *s2) && (*s1) && (--n))    {        s1++;        s2++;    }        return (*s1 -*s2);}

strstr、strrtr

char *strstr(char *s1, char *s2){    int len = strlen(s2);        while(*s1)    {        if(!strncmp(s1,s2,len))        {            return s1;        }        s1++;    }        return NULL;}
char *strrstr(char *s1,char *s2){    int len  = strlen(s2);        char *p = NULL;    while(*s1)    {        if(!strncmp(s1,s2,len))        {            p = s1;        }        s1++;    }        return p;}

squeeze、squeezestr

void squeeze(char *s, char c){    int  I = 0,j = 0;    while(s[i])    {        if(s[i] != c)        {            s[j++] = s[i];        }        i++;    }    s[j] = 0;}
void squeezestr(char *s1,char *s2){    int len = strlen(s2);        char *p = s1;    while (*s1)    {        if(!strncmp(s1,s2,len))        {            s1 = s1+len;        }        else        {            *p++ = *s1 ++;        }    }    *p = 0;}

bitcount

int bitcount(unsigned long n){    int num = 0;        while(n)    {        if(n &1)        {            num += 1;        }        n >>= 1;    }        return num;}

even_parity

int even_parity(unsigned long value,int n){         int count = 0, i;         for(I = 0; i
>=1; } Return (count%2 ==0);}

getbits、setbits

int getbits(unsigned long value, int p,int n){    unsigned long num = 0;        num = value <<(31-p);    num = num >>(32-n);             return num;}
int setbits(unsigned long *value, int p,int n, int x){    unsigned num1 = *value;    unsigned num2 = *value;        num1 = (num1 <<(p+1)) <<(p+1);    num2 = (num2 >>(31-p+n))>>(31-p+n);    x = (x <<(32-n))>>(31-p);    *value = num1|num2|x;        return *value;}

invert

int invert(unsigned long *value,int p,int n){    unsigned long temp = ~0;        temp = (temp <<(32-n))>>(31-p);    *value ^= temp;        return *value;}

reversebits

int reversebits(unsigned long *value){    int I = 0,j = 31;        for(; i<= j; i++,j--)    {        if(((*value >>j) &1) ^ ((*value >>i)&1))        {            *value ^= (1 >>j)|(1>>i);        }    }        return *value;}

rightrot

int rightrot(unsigned long *value,int n){    unsigned long num1 = *value <<(32-n);    unsigned long num2 = *value >>n;        *value = num1|num2;        return *value;}

swap

void swap(int *x,int *y){    *x = *x +*y;    *y = *x -*y;    *x = *x -*y;}

fac 、fib

int fac(int n) //implementd by iterative mothods.{    int res = 1;        while(n >1)    {        res = res *n;        n--;    }        return res;}
int fib(int n) //too{    int a = 1;    int b = 1;     int c = 0;        while(n >1)    {        n--;        c= b;        b= a;        a= b+c;    }        return a;}

printd

void printd(int n){    if(n / 10)    {        printd(n /10);    }        putchar(n % 10 + '0');}

大端

int is_bigendian(){    unsigned long test_num = 0x12345678;    if(*((short *)&test_num) == 0x1234)    {        fprintf(stderr, “bigendian”);        return 1;    }    else    {        fprintf(stderr, “littleendian”);    }        return 0;}

big_to_little16

int big_to_little16(unsigned short *x){    *x = (*x <<8)|(*x >.8);        return *x;}

big_to_little32

int big_to_little32(unsigned long *x){    *x = (*x >>24)|(*x &0x00ff0000)>>8 |(*x&0x0000ff00)<<8 |(*x<<24);             return *x;}

strspn

int stringspn(char *str,char *group){    int count = 0;        while(*str)    {        if(strchr(group, *str))        {            count ++;        }        else        {            break;        }        str++;    }        return count;}

strcspn

int stringcspn(char *str,char *group){    int count = 0;        while(*str)    {        if(!strchr(group, *str))        {            count++;        }        else        {            break;        }        str++;    }}

strpbk

char *stringpbk(char *str,char *group){    while(*str)    {        if(strchr(group, *str))        {            return str;        }        str++;    }        return NULL;}

memcpy

void *memcpy(void *dst, const void *src,int len){    char *p1 = (char *)dst;    char *p2 = (char *)src;        int i = 0;    for(i=0; i

memcmp

int *memcmp(const void *dst, const void *src,int len){    char *p1 = dst;    char *p2 = src;    int i = 0;        for (i=0; i

memchr

void *memchr(void *s,int ch,int len){    char *p = s;    int i = 0;        for(;i

memset

void *memset(void *s,int ch,int len){    char *p= s1;    int i = 0;    assert(NULL != s);        for(i=0; i

strdup

char *stringdup(char *str){    char *p = malloc(strlen(str) +1);        if(NULL == p)    {        perror("no memoty");        exit(1);    }    strcpy(p, str);        return p;}

getmemory

int getmemory(void **p, int size){    *p = malloc(size);        if(NULL != *p)    {        return -1;    }        return 0;}

转载于:https://www.cnblogs.com/chan-j/p/5995652.html

你可能感兴趣的文章
Algorithm——何为算法?
查看>>
Web服务器的原理
查看>>
常用的107条Javascript
查看>>
#10015 灯泡(无向图连通性+二分)
查看>>
linux设备驱动归纳总结(三):1.字符型设备之设备申请【转】
查看>>
《黑客与画家》 读书笔记
查看>>
bzoj4407: 于神之怒加强版
查看>>
mysql统计一张表中条目个数的方法
查看>>
ArcGIS多面体(multipatch)解析——引
查看>>
css3渐变画斜线 demo
查看>>
JS性能DOM优化
查看>>
设计模式 单例模式 使用模板及智能指针
查看>>
HAL层三类函数及其作用
查看>>
Odoo 去掉 恼人的 "上午"和"下午"
查看>>
web@h,c小总结
查看>>
java编程思想笔记(一)——面向对象导论
查看>>
Data Structure 基本概念
查看>>
Ubuntu改坏sudoers后无法使用sudo的解决办法
查看>>
NEYC 2017 游记
查看>>
[搬运] 写给 C# 开发人员的函数式编程
查看>>