常用函数编写
注意:最好的代码格式时候,我们还可以使用断言在函数的开始处检查参数的有效性。
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<n; i++)
{
if(max < arr[i])
{
max = arr[i];
}
}
return max;
}
int find_secondmax(int arr[], int n){
int I;
int max = arr[o];
int sec = arr[1];
if(arr[0] <arr[1]){
max = arr[1];
sec = arr[0];
}
For(I =2;i<n; i++){
If(arr[i] >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<n; i++){
if(value &1)
count ++;
value >>=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<len; i++)
{
p1[i] = p2[i];
}
return dst;
}
memcmp
int *memcmp(const void *dst, const void *src,int len)
{
char *p1 = dst;
char *p2 = src;
int i = 0;
for (i=0; i<len; i++)
{
if (p1[i] != p2[i])
{
return p1[i] - p2[i];
}
}
return 0;
}
memchr
void *memchr(void *s,int ch,int len)
{
char *p = s;
int i = 0;
for(;i<len;i++)
{
if(p[i] == ch)
return (void*)&p[i];
}
return NULL;
}
memset
void *memset(void *s,int ch,int len)
{
char *p= s1;
int i = 0;
assert(NULL != s);
for(i=0; i<len; i++)
{
p[i] = ch;
}
return s;
}
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;
}

浙公网安备 33010602011771号