码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • strcspn、strchr特殊字符校验


    strcspn、strchr特殊字符校验

    • 一、strcspn
      • 1、函数定义
      • 2、参数
      • 3、返回值
      • 4、demo
    • 二、strchr
      • 1、函数定义
      • 2、参数
      • 3、返回值
      • 4、demo
      • 5、为什么函数的“character ”参数是 int 类型?
    • 三、特殊字符校验
      • 1.需求
      • 2、demo
      • 3.输出

    一、strcspn

    1、函数定义

    size_t strcspn ( const char * str1, const char * str2 );

    2、参数

    str1 – 要被检索的 C 字符串。
    str2 – 该字符串包含了要在 str1 中进行匹配的字符列表。

    3、返回值

    该函数返回 str1 开头连续都不含字符串 str2 中字符的字符数。

    The length of the initial part of str1 not containing any of the characters that are part of str2.
    This is the length of str1 if none of the characters in str2 are found in str1.
    size_t is an unsigned integral type.

    4、demo

    /* strcspn example */
    #include 
    #include 
    
    int main()
    {
    	char str[] = "fcba73";
    	char keys[] = "1234567890";
    	int i;
    	i = strcspn(str, keys);
    	printf("The first number in str is at position %d.\n", i + 1);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    输出

    The first number in str is at position 5.

    参考:
    链接: https://cplusplus.com/reference/cstring/strcspn/
    链接: https://www.runoob.com/cprogramming/c-function-strcspn.html

    二、strchr

    1、函数定义

    const char * strchr ( const char * str, int character );
    char * strchr ( char * str, int character );

    2、参数

    str1 – 要被检索的 C 字符串。
    str2 – 该字符串包含了要在 str1 中进行匹配的字符列表。

    3、返回值

    该函数返回 str1 开头连续都不含字符串 str2 中字符的字符数。

    The length of the initial part of str1 not containing any of the characters that are part of str2.
    This is the length of str1 if none of the characters in str2 are found in str1.
    size_t is an unsigned integral type.

    4、demo

    #include 
    #include 
    
    int main(void)
    {
        char str[] = "I welcome any ideas from readers, of course.";
        char* lc = strchr(str, 'o');
        printf("strchr: %s\n", lc);
        char* rc = strrchr(str, 'o');
        printf("strrchr: %s\n", rc);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    输出

    strchr: ome any ideas from readers, of course.
    strrchr: ourse.

    5、为什么函数的“character ”参数是 int 类型?

    • 最后还需要注意的是,为什么函数的“c”参数是 int 类型,而不是“char”类型呢?

    • 其实原因很简单,这里用的是字符的 ASCII 码(因为每个字符都对应着一个 ASCII 码),这样在传值的时候既可以传“char”类型的值,又可以传“int”类型的值(0~127)。

    参考:
    链接: http://c.biancheng.net/view/401.html
    链接: https://cplusplus.com/reference/cstring/strchr/

    三、特殊字符校验

    1.需求

    • 1,不含有英文的特殊字符 “,./;[]<>?:{}| !@#$%^&*()_+=-'”\`~"
    • 2.首字符不可以为数字

    2、demo

    #include 
    static const char INVALD_CHARS[] = ",./;[]\<>?:{}|~!@#$%^&*()_+=-'\"\\`~";
    using namespace std;
    void check(char * input)
    {
        int len = strlen(input);
        if (strcspn(input, INVALD_CHARS) < len)
        {
            cout << "至少包含一个特殊字符"<<endl;
        }
        if (strchr("0123456789", input[0]) != nullptr)
        {
            cout << "首字符包含0123456789"<<endl<<endl;
        }
    }
    int main()
    {
        cout << "begin check input1"<<endl;
        char input1[] = "woaihecaicha";
        check(input1);
    
        cout << "begin check input2"<<endl;
        char input2[] = "woaihecaicha,";
        check(input2);
    
        cout << "begin check input3"<<endl;
        char input3[] = "9woaihecaicha";
        check(input3);    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    3.输出

    begin check input1
    begin check input2
    至少包含一个特殊字符
    begin check input3
    首字符包含0123456789
    在这里插入图片描述

  • 相关阅读:
    大数据必学Java基础(十八):条件运算符和位运算符
    【博学谷学习记录】超强总结,用心分享|架构师-RabbitMQ消息可靠性保障
    Duplicate entry ‘1559098812383174658‘ for key ‘rc_amt_contributions.PRIMARY‘
    vue3 watch & watchEffect
    C++新经典 | C++ 查漏补缺(类)
    Elasticsearch 完整指南
    Java String类(3):StringBulider和StringBuffer详解
    openMP学习笔记 -编程模型
    React组件、React脚手架、组件的props属性
    【Linux】使用 Alist 实现阿里云盘4K播放
  • 原文地址:https://blog.csdn.net/junxuezheng/article/details/125881457
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号