zoukankan      html  css  js  c++  java
  • 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;
    
    }
  • 相关阅读:
    linux默认的2.7升级到3.7版本
    linux 延时执行——at命令的几种用法
    unittest===unittest 的几种执行方式
    第一本docker书 学习笔记(二)
    第一本docker书 学习笔记(一)
    selenium===使用docker搭建selenium分布式测试环境
    https://www.yunpanjingling.com/
    AndroidManifest.xml权限设置
    XSS注入常用语句
    移动APP安全测试
  • 原文地址:https://www.cnblogs.com/kingshow123/p/stringoperation.html
Copyright © 2011-2022 走看看