zoukankan      html  css  js  c++  java
  • strcat()的编写

    1、strcat()

    #include <windows.h>
    #include <assert.h>
    #include <iostream>
    //strcat()函数分4部分写
    //1.定义4个char*
    //2.变量检查不为空assert()
    //3.指针指向第一个字符串的末尾
    //4.逐个字符的赋值
    char *strcat(char  *strDest, char *strSrc)
    {
        char *address = strDest;
        assert( (strDest != NULL)&&(strSrc != NULL));
        while(*strDest)
        {
            strDest++;
        }
        while(*strDest++ = *strSrc++)
        {
            ;
        }
        return address;
    }
    
    void main()
    {
        char l[10] ="li";    //注意此处需要时开辟空间的数组
        char *w = "wen";
        strcat(l,w);
        std::cout<<l;
    }
    View Code

     2、strcpy()

    #define  NULL 0
    #include <assert.h>
    #include <stdlib.h>
    #include <iostream>
    char * strcpy(char *strDest, char *strSrc)
    {
        char *address = strDest;
        assert(strDest!=NULL&&strSrc!=NULL);
        while (*strDest++ =  *strSrc++)
        {
            ;
        }
        return address;
    }
    void main()
    {
        char a[10] = "li";
        char *b="lwn";
        strcpy(a,b);
        std::cout<<a;
        system("pause");
    }
    View Code

     3、strcmp()

    #include <stdlib.h>
    #include <iostream>
    int strcmp(const char *strDest, const char *strSrc)
    {
        while (*strDest == *strSrc)
        {
            if (*strDest =='')        //注意时单引号
                return 0;
            strDest++;
            strSrc++;
        }
        return *strDest - *strSrc;
    
    }
    void main()
    {
        char a[10] = "li";
        char *b="li";
        strcmp(a,b);
        std::cout<<strcmp(a,b);
        system("pause");
    }
    View Code

     4.strlen()  //由于strlen()函数不加头文件也能调用下文将名字改为strln()

    #include <iostream>
    
    int strln(const char *strDest)
    {
        int i=0;
        while (*strDest !='')
        {
            strDest++;i++;
        }
        return i;
    }
    
    void main()
    {
        char a[10] = "li4";
        std::cout<<strln(a);
        system("pause");
    }
    View Code
  • 相关阅读:
    AmazonS3 替换HDFS 方案
    SQL Server 内存管理
    SQL Server I/O 问题的诊断分析
    共享内存 设计原理-shm
    机器学习经典算法笔记-Support Vector Machine SVM
    ACGAN 论文笔记
    CGAN 论文笔记
    《Image Generation with PixelCNN Decoders》论文笔记
    《Iterative-GAN》的算法伪代码整理
    《Deep Learning Face Attributes in the Wild》论文笔记
  • 原文地址:https://www.cnblogs.com/lwngreat/p/4257346.html
Copyright © 2011-2022 走看看