Simple implementation of several string library functions in c

Source: Internet
Author: User

Simple implementation of several string library functions in c

 #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;}/*************pabcdes-----s1234567    *************/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 \0while(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;}  }}//44void *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));}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.