zoukankan      html  css  js  c++  java
  • strcasecmp()函数

    函数介绍:

    strcasecmp用忽略大小写比较字符串.,通过strcasecmp函数可以指定每个字符串用于比较的字符数,strncasecmp用来比较参数s1和s2字符串前n个字符,比较时会自动忽略大小写的差异。

    strcasecmp函数是二进制且对大小写不敏感。此函数只在Linux中提供,相当于windows平台的 stricmp。

     

    函数声明:

    #include<strings.h>(不是C/C++标准头文件,区别于string.h)
    int strcasecmp(const char *s1, const char *s2);

     

    返回值:

    若参数s1和s2字符串相等返回0,s1大于s2则返回大于0的值,s1小于s2则返回小于0的值。

     

    实例:

    /*
    strcasecmp.c
    */
    #include<strings.h>
    #include<stdio.h>
    
    int main()
    {
        char *s1 = "aBcDeD";
        char *s2 = "AbCdEd";
        char *s3 = "abcdefg";
        char *s4 = "bacdefg";
    
        int len = strcasecmp(s1, s2);
        if (!len)
        {
            printf("%s = %s", s1, s2);
        }
        else
        {
            printf("%s != %s", s1, s2);
        }
        
    
         len = strcasecmp(s1, s3);
        if (!len)
        {
            printf("%s = %s", s1, s3);
        }
        else
        {
            printf("%s != %s, len = %d
    ", s1, s3, len);
        }
    
         len = strcasecmp(s1, s4);
        if (!len)
        {
            printf("%s = %s", s1, s4);
        }
        else
        {
            printf("%s != %s, len = %d
    ", s1, s4, len);
        }
    
        return 0;
    
    }

    运行结果:

    aBcDeD = AbCdEdaBcDeD != abcdefg, len = -2

    aBcDeD != bacdefg, len = -1

     

    补充:

    strncasecmp()只比较前n个字符

    函数定义

    int strncasecmp(const char *s1, const char *s2, size_t n)
  • 相关阅读:
    Django 标签过滤器
    Python短路原则
    python学习之路 八 :面向对象编程基础
    python学习之路 七 :生成器、迭代器
    python学习之路 六 :装饰器
    python学习之路 五:函数式编程
    python学习之路 四 :文件处理
    python学习之路 三:字符编码
    机器学习流程管理
    pyspark 自定义聚合函数 UDAF
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/12054962.html
Copyright © 2011-2022 走看看