zoukankan      html  css  js  c++  java
  • 自定义C语言中常用的字符串操作函数

    C语言中提供了许多的字符串操作函数,常见的字符串操作函数有以下几种:

    1,求字符串长度的函数

      原型函数:strlen(字符串名称);

      实现原理:将字符串名称传入该函数,该函数会遍历该字符串,最后将长度返回给我们,注意返回的长度不包括'';

    2,字符串拷贝函数

      原型函数:strcpy(字符串1名称, 字符串2名称);

      实现原理:该函数需要传入两个字符串名称,所谓拷贝其实是指将字符串2所包含的元素,复制到字符串1中,注意

      这里的复制其实也包含覆盖的意思,字符串1中原来的元素会被覆盖掉;

    3,字符串拼接函数

      原型函数:strcat(字符串1名称, 字符串2名称);

      实现原理:该函数也需要传入两个字符串名称,所谓拼接其实是指将字符串2所包含的元素,拼接到字符串1的后面;

    4,字符串比较函数

      原型函数:strcmp(字符串1名称, 字符串2名称);

      实现原理:该函数也是需要传入两个字符串的名称的,然后同时遍历两个字符串,每次都分别从这两个字符串中取出一个字符,

      再比较这两个字符的ASCII码,如果从字符串1中取出的字符的ASCII码比从字符串2中取出的ASCII码要大,那么该函数就会返回 1,

      如果小的话该函数就会返回 -1,如果取到了两个字符串的最后一个字符,每一个字符的ASCII码都是相等的,那么该函数就会返回 0。

    了解完这几个字符串操作函数的原理之后,我们就可以不使用C语言提供的库函数,自己自定义这几个函数了;

    具体代码如下:

    #include <stdio.h>
    //函数声明
    int myStrlen(char str[]);
    void muStrcpy(char str1[], char str2[]);
    void myStrcat(char str1[], char str2[]);
    int myStrcmp(char str1[], char str2[]);
    int main()
    {
        char str1[] = "TomHe789";
        char str2[] = "TomHe";
        char str3[] = "789";
        char str4[] = "abcde";
        char str5[] = "abcde";
        //打印str1的长度
        printf("str1Len = %d
    ", myStrlen(str1));   //8
        //将str2拷贝到str1中
        muStrcpy(str1, str2);                       
        printf("str1 = %s
    ",str1);                 //TomHe
        //将str3拼接到str2后面
        myStrcat(str2, str3);                       
        printf("str2 = %s
    ",str2);                 //TomHe789
        //打印出 myStrcmp(str4, str5)的结果
        printf("res = %d", myStrcmp(str4, str5));   //0
        return 0;
    }
    
    //自定义字符串长度函数
    int myStrlen(char str[]){
        int len = 0;
        while (str[len] != '')
        {
            len++;
        }
        return len;
        
    }
    
    //自定义字符串拷贝函数
    void muStrcpy(char str1[], char str2[])
    {
        int len = 0;
        while (str2[len] != '' || str1[len] != '')
        {
            str1[len] = str2[len];
            len++;
        }
        str1[len] = '';
        
    }
    
    //自定义字符串拼接函数
    void myStrcat(char str1[], char str2[])
    {
        int len = 0;
        int len1 = 0;
        while (str1[len] != '')
        {
            len++;
        }
        while (str2[len1] != '')
        {
            str1[len] = str2[len1];
            len++;
            len1++;
        }
        str1[len] = '';
    }
    
    //自定义字符串比较函数
    int myStrcmp(char str1[], char str2[])
    {
        int len = 0;
        while (str1[len]!='' || str2[len]!='')
        {
            if(str1[len] > str2[len])
                return 1;
            else if(str1[len] < str2[len])
                return -1;
            else
                len++;
        }
        return 0;
    }

  • 相关阅读:
    hi.baidu.com 百度流量统计
    Autofac is designed to track and dispose of resources for you.
    IIS Manager could not load type for module provider 'SharedConfig' that is declared in administration.config
    How to create and manage configuration backups in Internet Information Services 7.0
    定制swagger的UI
    NSwag在asp.net web api中的使用,基于Global.asax
    NSwag Tutorial: Integrate the NSwag toolchain into your ASP.NET Web API project
    JS变量对象详解
    JS执行上下文(执行环境)详细图解
    JS内存空间详细图解
  • 原文地址:https://www.cnblogs.com/TomHe789/p/12452492.html
Copyright © 2011-2022 走看看