• C语言 stringcpy,stringcat,stringcmp实现


          复习复习C语言,O(∩_∩)O~

    #include<stdio.h>
    #include<conio.h>
    
    //实现从源字符串string到目的字符串str的复制函数
    char* stringCpy(char* str,const char* string)
    {
        char* s=str;
        while(*string)
        {
            *s++=*string++;
        }
        *s='';
        //返回目的字符串的首地址
        return str;
    }
    
    //函数将字符串string链接到字符串str的尾部
    char* stringCat(char* str,const char* string)
    {
        char* s=str;
        //找到字符串str的尾部
        while(*s)
        {
            s++;
        }
        while(*string)
        {
            *s++=*string++;
        }
        *s='';
        //返回目的字符串的首地址
        return str;
    }
    
    //比较大小的函数
    //实现两个字符串str和string的比较
    //如果str小于string返回负值,如果str大于string返回正直,如果str等于string返回0
    int stringCmp(const char* str,const char* string)
    {
        while((*str)&&(*string)&&(*str==*string))
        {
            str++;
            string++;
        }
        return (int)(*str-*string);
    }
    
    int main()
    {
        char s1[20];
        const char* s2="abc";
        const char* s3="def";
        char* pc;
        int cmp;
        puts("**************************************");
        puts("|   The program will complish:       |");
        puts("|   strcpy,strcat,strcmp             |");
        puts("**************************************");
        printf("The string s2 is:%s
    ",s2);
        printf("The string s3 is:%s
    ",s3);
        pc=stringCpy(s1,s2);
        printf("This is stringcpy s2 to s1,s1 is :
    ");
        puts(pc);
        pc=stringCat(s1,s3);
        printf("This is stringcat s1 to s3,s1 is :
    ");
        puts(pc);
        cmp=stringCmp(s2,s3);
        if(cmp==0)
            printf("
    The string s2 is equal to s3
    ");
        else if(cmp<0)
            printf("
    The string s2 is smaller to s3
    ");
        else
            printf("
    The string s2 is larger to s3
    ");
        getch();//从控制台读取一个字符,但不显示在屏幕上,实现在该位置暂停一下,按任意键继续
        return 0;
    
    }
  • 相关阅读:
    开源项目
    分享知识 学无止境 只做正确的事 伸出援助之手
    公开支持与鼓励,私下质疑与建议(转)
    天使投资人给阿里新贵们的一些建议(转)
    沟通中“倾听”的五个层次
    intent
    SafeNet推出行业首款白盒password软件保护解决方式
    应用系统设计思考
    常见的几种RuntimeException
    ASP.NETserver控件使用之Reportviewer 报表
  • 原文地址:https://www.cnblogs.com/kingshow123/p/stringoperation.html
Copyright © 2020-2023  润新知