当前位置:网站首页>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的任一字符时,返回字符串长度。
边栏推荐
- C language ---9 first knowledge of macros and pointers
- Abbexa AML1 DNA binding ELISA Kit instructions
- 01js basic null and undefined difference type conversion = = code block logical operator
- 数据库系统概论 ---- 第一章 -- 绪论(重要知识点)
- Leetcode advanced road - 125 Validate palindrome string
- Redis cache breakdown
- Summary of common mysql8 commands in centos7 environment
- ThinkPHP v6.0.x反序列化漏洞复现
- Rotate menu 3.0
- 【北大青鸟昌平校区】职教与普教协调发展,今年的中考会容易吗?
猜你喜欢

Course design of imitation pottery ticket of wechat applet

2022 - 06 - 09 rk817 PMU Battery Temperature Detection

Exec function of PHP
php伪协议实现命令执行详情

解读创客空间下的教育新生态

C language ---5 initial string, escape character and comment

Practical | how to use burp suite for password blasting!

【Microsoft Azure 的1024种玩法】七十五.云端数据库迁移之快速将阿里云RDS SQL Server无缝迁移到Azure SQL Databas

Detailed explanation of Lora module wireless transceiver communication technology

SQL第四练:字符串处理函数
随机推荐
C language ---5 initial string, escape character and comment
Extracting Nalu unit data from H264 real-time stream
JS anchor positioning can extend many functions
旋转菜单3.0
[1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas
I'm doing something cool
Introduction to ZigBee module wireless transmission star topology networking structure
MySQL数据库如何查看表占用空间大小
初中毕业生,选择中职学校也可以升入高等学府
Standard dual airbags, starting from 48900 for butcher Chang'an Lumin
Redis cache penetration
Forward slash "/", backslash "\," escape character "\" and file path separator cannot be clearly remembered
Mysql之將查詢結果插入到其它錶中
Shell implements SSH login and executes commands
C language -- 7 operators
Video monitoring system storage control, bandwidth calculation method
【MySQL】表数据的增删查改(DML)
SQL Server2019安装的详细步骤实战记录(亲测可用)
H265 Nalu type judgment and SPS data analysis
CCF class a conference or journal - regression related papers