常见递归&非递归实现
void my_strcpy(char *to,const char *from)
{
if('\0' == *from){
*to = '\0';
return ;
}
*to++ = *from++;
my_strcpy(to,from);
}
//只拷贝n个字符
void my_strncpy(char *to,const char* from,int n)
{
if( 0 == n || '\0' == *from){
*to = '\0';
return ;
}
*to++ = *from++;
my_strncpy(to,from,n-1);
}
//字符串比较
int my_strcmp(const char *src,const char *dest)
{
if((NULL == src || NULL == dest))
return err;
if('\0'==*src && '\0'== *dest)
return 0;
if(*src != *dest)
return ( ((*src - *dest)>0)? 1 : -1 );
my_strcmp(++src,++dest);
}
//之比较n个字符
int my_strncmp(const char* src,const char *dest,int n)
{
if((NULL == src || NULL == dest))
return err;
if(0 == n)
return 0;
if(*src != *dest)
return ( ((*src-*dest)>0)? 1 : -1 );
my_strncmp(++src,++dest,--n);
}
//过滤掉重复的字符
void word_filter(char *src)
{
if(*src == '\0')
{
printf("\n");
return ;
}
int index = *src;
if(tmp[index] == 0){
tmp[index]++;
printf("%c",*src);
}
word_filter(++src);
}
//字符包含问题(小写字符)
int str_include(const char *s1,const char *s2)
{
if(NULL == s1 || NULL == s2 )
return err;
enum ret no=n;
enum ret yes=y;
unsigned int bit_map = 0x0;
for( ; *s2 ; s2++)
bit_map |= 0x1 << (*s2 - 'a');
for( ; *s1 ; s1++)
if(bit_map != (bit_map |= (0x1<<(*s1 - 'a'))))
return no;
return yes;
}
//找数组中的最大值
int find_max_value(int *arr,int num)
{
if(1==num)
return arr[0];
int max = find_max_value(arr+1,num-1);
if(max<arr[0])
max=arr[0];
return max;
}
//字符串转置
void str_nizhi(char *str,int start,int end)
{
if(start>=end)
return ;
char ch=*(str+start);
*(str+start) = *(str+end);
*(str+end) = ch;
str_nizhi(str,start+1,end-1);
}
//利用循环求字符串的长度
int fun_strlen(const char *src)
{
int len=0;
for( ; *src++; len++);
return len;
}
//递归实现字符串的长度统计
int ret_str_len(const char *src)
{
if(*src=='\0')
return 0;
return (ret_str_len(++src)+1);
}
//统计ch 中1的个数
int ch_tell(unsigned char ch)
{
if(ch)
{
if(ch&0x01)
return (ch_tell(ch>>1)+1);
else
return ch_tell(ch>>1);
}
else
return 0;
}
//如ch :0x00 到 0xff (0~255)中有多少偶数个1(00000000:0个1、00001001:2个1)的数
int fun_tell(void)
{
unsigned char ch = 0x0;
int sum = 0;
for( ; ; )
{
if(ch_tell(ch)%2 == 0)
sum++;
if(ch == 0xff)
break ;
ch = ch+1;
}
return sum;
}
//找数组中相同的值(数值不能大于数组的大小)
#if 1
int get_same_value1(int *arr,int len)
{
if(NULL == arr || len<=0)
return err;
int i=0, *new_array =(int *)malloc(sizeof(int)*len);
if(new_array == NULL)
return err;
//memset(new_array,-1,len);
for( ; i<len; i++)
new_array[i]=-1;
for( i=0 ; i<len ; i++)
{
if(new_array[arr[i]] == -1)
new_array[arr[i]] = arr[i];
else
return arr[i];
}
free(new_array);
return 0;
}
#endif
int get_sum(int a, int b)
{
if(b == 0)
return a;
int aa = a^b;
int bb = (a&b)<<1;
return get_sum(aa,bb);
}
void zhe_ban_insert_sort(int *arr,int len)
{
if(NULL == arr || len<=0)
return ;
int tmp = 0;
int i = 0,start=0,end=0,middle=0,j=0;
for(i=1 ; i<len ; i++)
{
tmp = arr[i];
start = 0;
end = i-1;
while(start<=end)
{
middle = (start+end)/2;
if(arr[middle]>arr[i])
end = middle-1;
else
start=middle+1;
}
for(j=i-1; j>=start; j--)
arr[j+1]=arr[j];
arr[start]=tmp;
}
}
static inline void get_size(void)
{
char str[] = "hello";
char *pt = str ;
char *ptr= NULL;
void *p = malloc(100);
int i =10;
printf("sizeof str is %d\nstrlen of str is %d\n",sizeof(str),fun_strlen(str));//6,5
printf("sizeof pt is %d\nstrlen of pt is %d\n",sizeof(pt),fun_strlen(pt));//4,5
printf("sizeof ptr is %d\n",sizeof(ptr));//4
printf("sizeof p is %d\n",sizeof(p));//4
printf("sizeof i is %d\n",sizeof(i));//4
return ;
}
void *str_str(const char *s1,const char *s2,int (*ncmp)(const char *,const char *,int))
{
if(NULL == s2 || s2 == NULL)
return NULL;
int len1 = ret_str_len(s1);
int len2 = ret_str_len(s2);
if(len1<len2)
return NULL;
int i = 0;
for( ; i<len1&&(len1-i>=len2) ; i++){
if(!ncmp(s1+i,s2,len2))
return s1+i;
}
return NULL;
}