码农知识堂 - 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
    在这里插入图片描述

  • 相关阅读:
    segment方案解决VXLAN分布式网关DCI间互联
    10 个用于网络管理员进行高级扫描的端口扫描工具
    万达商业再递招股书:上半年派息35亿 腾讯与碧桂园是股东
    【基站维修工程师】python实现-附ChatGPT解析
    编写一个kubernetes controller
    mysql数据库概述及安装
    常用文件名后缀一览
    剑指 Offer 32 - I. 从上到下打印二叉树(java解题)
    React技术栈 --》文件模块化和按钮绑定事件 ## Day5
    解决安装sentry执行install.sh卡住的问题
  • 原文地址:https://blog.csdn.net/junxuezheng/article/details/125881457
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号