c的几个string库函数简单实现

页面导航:首页 > 软件编程 > C 语言 > c的几个string库函数简单实现

c的几个string库函数简单实现

来源: 作者: 时间:2016-01-25 09:50 【

#includestdio.h#includestdlib.h#includestring.h#includemalloc.hint fun1(char *p){ int len=0;while(*p!=0){len++;p++;} return len;}int fun2(const char *s){ const char *...
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
int fun1(char *p)
{
  int len=0;
while(*p!=0){len++;p++;}
  return len;
}
int fun2(const char *s)
{   const char *p=s;
   while(*p++);
    return p-s-1;
}
/*************
p
abcde
s-----s
1234567    
*************/
int fun3(const char *s)
{ if(*s==0)return 0;
  else return(1+fun3(s+1));
}
void *fun4(char *d,const char*s,int n)
{//assert(d);assert(s);
 char *ret=d;
while(*d){d++;}//d move to \0
while(n)       
{*d++=*s++;n--;}//\0\0\0 turn to abcde....
return ret;
 
}
void *fun5(  char *d,  char *s)
{
  char *ret=NULL;
  while(*d)
  {
    while(*s){if(*d==*s){d++;s++;}else break;}
    if(*s==0) return ret;
    else {d++;ret=d;}
  }
}//44
void *fun6(  char *s1,  char *s2)
{
char *pstr=s1;
char *p1=NULL;
char *p2=NULL;
  while(*pstr)
  {  p1=pstr;p2=s2;
    while((*p1)&&*p1==*p2){p1++;p2++;if(*p2==0)return pstr;}
     pstr++;
    
  }
return 0;
}
char *fun7(char *d,const char *s)
{
char *ret=d;
while(*d++=*s++);
return ret;
}
/*******
s1<--s2
*******/
int fun8(const char* s1,const char *s2)
{
while(*s1==*s2){if(*s1==0)return 0; s1++;s2++;}
 return (*s1-*s2);
}
/*******
s1>s2  return  1 2 3....
*******/
void *fun9(void *d,const void *s,int size)
{
void *ret=d;
char *p1=(char *)d;
char *p2=(char *)s;
while(size--){*p1++;*p2++;}
return ret;
}
void *fun10(void *d,const void *s,int size)
{
void *ret=d;
char *p1=(char *)d;
char *p2=(char *)s;
if(p1>p2&&p1<p2+size)
{
while(size--){*(p1+size)=*(p2+size);}
}
else while(size--){*p1++=*p2++;}
return ret;
}


void main()
{
char ss[]="abcde";
char s1[]="abcde";
char s2[]="abcde";
char *p=ss;
 //printf("%d\n",strlen(p));
 //printf("%d\n",fun1(p));
 //printf("%d\n",fun2(p));
 //  printf("%d\n",fun3(p));
//   printf("%s\n",fun4(s1,s2,1));//have bug printf more about s2;
//   printf("%s\n",fun6(s1,s2));
//   printf("%s\n",fun7(s1,s2));
//   printf("%d\n",fun8(s1,s2));
printf("%s\n",fun9(s1,s2,5));
printf("%s\n",fun10(s1,s2,5));
}

 

Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<