当前位置:网站首页>C中字符串查找
C中字符串查找
2022-06-10 20:50:00 【sinat_41752325】
目录
字符串查找分为:在字符串中查找单个字符,在字符串中查找多个字符,在字符串中查找子串。
查找单个字符使用strchr与strrchr;
查找多个字符使用strpbrk;
查找子串使用strstr;
strspn和strcspn,逐个检查两个字符串是否相同
和其它字符串处理函数一样,使用这些函数时需要包含头文件 <string.h>
1. 查找单个字符 strchr/strrchr
strchr与strrchr函数声明如下:
char *strchr(const char *s, int c);
char *strrchr(const char *s, intc);这两个函数都是从字符串s中查找字符c,如果找到字符c就返回,返回值指向这个位置,如果没有找到字符c,返回NULL。
strchr与strrchr函数的不同在于:strchr从左向右查找字符,strrchr从右向左查找字符。
使用示例,在字符串str中查找字符c并打印结果:
char *str = "When we let go of something.it opens up a little space to grow.";
int c = 'g';
char *ppos = strchr(str,c);
char *ppos1 = strrchr(str,c);
printf("str\t\t= %s\n",str);
printf("strchr ret\t= %s\n",ppos);
printf("strrchr ret\t= %s\n",ppos1);输出结果如下:
str = When we let go of something.it opens up a little space to grow.
strchr ret = go of something.it opens up a little space to grow.
strrchr ret = grow.
2. 查找多个字符中任一字符 strpbrk
strpbrk函数声明:
char *strpbrk(const char *s1, const char *s2);strpbrk函数在源字符串s1中按从前往后的顺序找出最先含有字符串s2中任一字符的位置并返回,不包含'\0',如果找不到返回空指针。函数定义如下:
char *strpbrk(const char *s1, const char *s2)
{
while( *s1 != '\0')
{
const char *a = s2;
while(*a != 0)
{
if(*a++ == *s1)
return (char*)s1;
++s1;
}
}
}使用示例:
char *str = "When we let go of something.it opens up a little space to grow.";
char* str2 = "met";
char *ppos2 = strpbrk(str,str2);
printf("str\t\t= %s\n",str);
printf("strpbrk ret\t= %s\n",ppos2);
输出结果:
str = When we let go of something.it opens up a little space to grow.
strpbrk ret = en we let go of something.it opens up a little space to grow.
3. 查找一个子串 strstr
strstr函数声明:
char *strstr(const char *str1, const char *str2);strstr函数在字符串str1中从左到右查找str2,str1连续包含str2中的所有字符时,返回str1中第一次出现str2的位置的指针,如果没有找到,返回NULL。
使用示例:
char *str = "When we let go of something.it opens up a little space to grow.";
char* str2 = "met";
char *ppos3 = strstr(str,str2);
printf("str\t\t= %s\n",str);
printf("strstr ret\t= %s\n",ppos3);
输出结果:
str = When we let go of something.it opens up a little space to grow.
strstr ret = mething.it opens up a little space to grow.
4.逐个检查两个字符串 strspc/strcspn
strspn和strcspn函数声明:
size_t strspn(const char *s,const char *accept);
size_t strcspn(const char *s,cosnt char *reject);strspn从字符串s的第一个字符开始,逐个检查与字符串accept中的字符是否不相同,如果不相同,停止检查,返回以字符串s开头连续包含accept内的字符的数目;
strcspn从字符串s的第一个字符开始,逐个检查与reject中的字符是否相同,如果相等,停止检查,返回以字符串s开头连续不含字符串reject内的字符数目,
使用示例:
char *str = "When we let go of something.it opens up a little space to grow.";
char *str3 = "bdjk";
size_t pos1 = strspn(str,str3);
size_t pos2 = strcspn(str,str3);
printf("%s = %d\n",str3,pos1);
printf("%s = %d\n",str3,pos2);
char *str4 = "When";
pos1 = strspn(str,str4);
pos2 = strcspn(str,str4);
printf("%s = %d\n",str4,pos1);
printf("%s = %d\n",str4,pos2);输出结果:
str = When we let go of something.it opens up a little space to grow.
bdjk = 0
bdjk = 63
When = 4
When = 0
使用strspn和strcspn时需要注意:
使用strspn时,当第一个字符不相同或者字符串s不包含accept中的任一字符时,返回0;
使用strcspn时,当第一个字符相同时,返回0;当字符串s不包含accept的任一字符时,返回字符串长度。
边栏推荐
- 【phpstorm】 No data sources are configured to run this SQL and provide advanced c
- Standard dual airbags, starting from 48900 for butcher Chang'an Lumin
- MySQL数据库如何查看表占用空间大小
- Qingniao Changping campus of Peking University: can I learn UI with a high school degree?
- [nk] Niuke monthly competition 51 f-average question
- Will your company choose to develop data center?
- JS mobile terminal copy text to clipboard code
- SQL第四练:字符串处理函数
- 构建幼儿Steam教育实施策略
- C language -- 7 operators
猜你喜欢

Standard dual airbags, starting from 48900 for butcher Chang'an Lumin
php伪协议实现命令执行详情

What do software test engineers do?
一次SQL查询优化原理分析(900W+数据从17s到300ms)

Interview Essentials - basic knowledge of synchronized underlying principles

Introduction to ZigBee module wireless transmission star topology networking structure

Introduction to database system -- Chapter 1 -- Introduction (important knowledge points)
SQL Server2019安装的详细步骤实战记录(亲测可用)

Abbexa cell free DNA kit instructions

C language -- 7 operators
随机推荐
[nk] 牛客月賽51 G計算題
目标检测相关概念的理解
What do software test engineers do?
How to realize the marketing purpose of small program integral mall
Redis cache avalanche
Course design of imitation pottery ticket of wechat applet
H. Relationship among Nalu, RBSP and sodb in 264
CentOS7环境下MySQL8常用命令小结
AI blessing real-time interaction | analysis of zegoavatar facial expression following technology
NFT版权/版税
Constructing the implementation strategy of steam education for children
As a programmer, is it really that important for the underlying principles?
旋转菜单2.0
自制Table錶格
"O & M youxiaodeng" self service account unlocking tool
Junior high school graduates who choose secondary vocational schools can also be promoted to institutions of higher learning
C language ---6 first knowledge of selection statement, loop statement, function and array
01js basic null and undefined difference type conversion = = code block logical operator
入行须知:运维需要懂编程吗?
PHP pseudo protocol implementation command execution details