自定义strcmp,strlen,strcpy,strcat函数

#include<stdio.h>

char *strcpy(char *dest,const char *src){
    char *temp=dest;
    while((*temp++=*src++)!='\0');
    return dest;
}
size_t strlen(const char *s){
    size_t n=0;
    while(*s++)n++;
    return n;
}
char *strcat(char *s1,const char *s2){
    char *p=s1;
    while(*p)p++;
    while(*p++=*s2++);
    return s1;
}
int strcmp(const char *s1,const char *s2){
    while(*s1==*s2){
        if(*s1=='\0')return 0;
        s1++;s2++;
    }
    return *s1-*s2;
}
void test01(){
    char s1[]="he llo";
    //char s1[10];
    //char *s1="he llo";错 
    //char *s1;错
    char *s2=" world";
    //char s2[]=" world";
    //char s2[10];
    strcpy(s1,s2);
    printf("%s",s1);    
}
void test02(){
    char s1[]="he llo";
    //char *s1="he llo";
    //char s1[10];
    //char *s1;错
    printf("%d",strlen(s1));
}
void test03(){
    char s1[]="he llo";
    //char s1[10];
    //char *s1="he llo";错 
    char *s2=" world";
    //char s2[]=" world";
    //char s2[10];
    strcat(s1,s2);
    printf("%s",s1);    
}
void test04(){
    char *s1="abc";
    char *s2="abd";
    printf("%d",strcmp(s1,s2)<0);
}
int main(){
    test04();
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/lyt888/p/12594947.html
今日推荐