当前位置:网站首页>Strcspn, strchr special character verification
Strcspn, strchr special character verification
2022-07-21 19:23:00 【junxuezheng】
strcspn、strchr Special character check
One 、strcspn
1、 Function definition
size_t strcspn ( const char * str1, const char * str2 );
2、 Parameters
str1 – To be retrieved C character string .
str2 – The string contains the string to be used in str1 A list of characters to match in .
3、 Return value
This function returns str1 There is no string at the beginning str2 Number of characters in .
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 <stdio.h>
#include <string.h>
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;
}
Output
The first number in str is at position 5.
Reference resources :
link : https://cplusplus.com/reference/cstring/strcspn/
link : https://www.runoob.com/cprogramming/c-function-strcspn.html
Two 、strchr
1、 Function definition
const char * strchr ( const char * str, int character );
char * strchr ( char * str, int character );
2、 Parameters
str1 – To be retrieved C character string .
str2 – The string contains the string to be used in str1 A list of characters to match in .
3、 Return value
This function returns str1 There is no string at the beginning str2 Number of characters in .
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 <stdio.h>
#include <string.h>
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;
}
Output
strchr: ome any ideas from readers, of course.
strrchr: ourse.
5、 Why is the function “character ” Parameter is int type ?
Last but not least , Why is the function “c” Parameter is int type , instead of “char” Type? ?
The reason is simple , Here we use character ASCII code ( Because each character corresponds to a ASCII code ), In this way, when transferring values, you can transfer “char” Type value , You can pass it on again “int” Type value (0~127).
Reference resources :
link : http://c.biancheng.net/view/401.html
link : https://cplusplus.com/reference/cstring/strchr/
3、 ... and 、 Special character check
1. demand
- 1, No special characters in English “,./;[]<>?:{}| [email protected]#$%^&*()_+=-'”\`~"
- 2. The first character cannot be a number
2、demo
#include <iostream>
static const char INVALD_CHARS[] = ",./;[]\<>?:{}|[email protected]#$%^&*()_+=-'\"\\`~";
using namespace std;
void check(char * input)
{
int len = strlen(input);
if (strcspn(input, INVALD_CHARS) < len)
{
cout << " Contains at least one special character "<<endl;
}
if (strchr("0123456789", input[0]) != nullptr)
{
cout << " The first character contains 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);
}
3. Output
begin check input1
begin check input2
Contains at least one special character
begin check input3
The first character contains 0123456789
边栏推荐
猜你喜欢
TiDB 分布式批量解决方案
腾讯IM实战:低代码超快实现即时通讯录
h5在微信内自定义分享遇到的坑
FTP service configuration
[performance optimization] MySQL common slow query analysis tools
寻找单身狗--重复两次数字中寻找单个数字
Phpstudy_ Pro builds sqli labs shooting range for SQL injection test
Spark RDD, application case of spark SQL
程序员第一课“hello word”,你知道网工第一课吗?
【LeetCode】1260. 2D mesh migration
随机推荐
Practice and exploration of knowledge atlas question answering Technology
分布式.数据库架构
How to realize copyright protection of Digital Collections
分布式.高并发&高可用
MYSQL09_精讲数据库数据类型
SAP smartforms 打印失败 消息类型:SSFCOMPOSER 消息号:601 (货币和数字字段设置参考及格式)
QT (37) -mosquitto mqtt client
JSP自定义标签(foreach标签与select标签的自定义方式)
C语言文件操作
如何解决wget命令出现Unable to establish SSL connection.
Software testing interview question: what is your biggest interest in testing? Why?
Arduino I2C for TCA9548A应答扫描程序
MYSQL06_ Seven join operations and union all of sql99
【LeetCode】1260. 2D mesh migration
[experience sharing] mathematical modeling team prepares solution process strategy sharing
分布式.高并发概念和设计目标
L1-003 个位数统计
Force uninstall of additional domain controllers
[MCU simulation project] 4 × 4 matrix keyboard scanning
函数、方法和接口的区别