C 简陋版string操作strcpy strcmp strcat strchr strstr

空扰寡人 提交于 2019-12-09 11:39:02

文件下载地址:http://pan.baidu.com/s/1bn2BcTL

代码如下:


#include <stdio.h>

#include <stdlib.h>



char *strcpy_(char *dest,const char *src);

char *strcat_(char *dest,const char *src);

int strcmp_(const char *dest,const char *src);

int strlen_(const char *src);

char *strchr_(char *s, int c);

char *strstr_(const char *s,const char *c);

int main()

{

char p[]="xxdexxx";

char q[]="de";

printf("p=%s\n",p);

printf("q=%s\n",q);

printf("strlen_(p)=%d\n",strlen_(p));

printf("strcpy_(p,q)=%s\n", strcpy_(p,q));

char p1[]="xxdexxx";

char q1[]="de";

printf("strchr_(p,'d')=%s\n",strchr_(p1,'d'));

char p2[]="xxdexxx";

char q2[]="de";

printf("strstr_(p,q)=%s\n",strstr(p2,q2));

char p3[]="xxdexxx";

char q3[]="de";

printf("strcmp_(p,q)=%d\n",strcmp_(p3,q3));

char p4[]="xxdexxx";

char q4[]="de";

printf("strcat_(p,q)=%s\n",strcat_(p4,q4));

return 0;

}


char *strstr_(const char *s,const char *c)

{

const char *p=NULL;

const char *q=NULL;

while(*s!='\0')

{

p=s;

p=c;

while(*s==*c&&*c!='\0')

{

s++;

c++;

}

if(*c=='\0')

{

return (char*) p;

}

p++;

c=p;

s=p;

}

return NULL;

}

char *strcpy_(char *dest,const char *src)

{

char *p=dest;

while(*src!='\0')

{

*p=*src;

p++;

src++;

}

return dest;

}


int strcmp_(const char *dest,const char *src)

{

int result=0;

const char *p=dest;

AA: if(*p==*src)

{

p++;

src++;

goto AA;

}else

{

result=*p-*src;

return result;

}

return result;


}


char *strcat_(char *dest,const char *src)

{

int len=strlen_(dest)+strlen_(src)+1;

char *p=(char *)malloc(len);

if(p!=NULL)

{

strcpy_(p,dest);

strcpy_(p+strlen_(dest),src);

return p;

}

return NULL;


}


char *strchr_(char *s, int c)

{

char *p=s;

while(*p!='\0')

{

if(*p==c)

{

return p;

}

p++;

}

return NULL;


}


int strlen_(const char *src)

{

int count=0;

const char *p=src;

while(*p++)

{

count++;

}

return count;

}


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!