首页 > 其他 > 详细

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

时间:2020-03-29 22:04:22      阅读:63      评论:0      收藏:0      [点我收藏+]
#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;
} 

 

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

原文:https://www.cnblogs.com/lyt888/p/12594947.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!