当前位置:网站首页>String search in C
String search in C
2022-06-10 22:12:00 【sinat_ forty-one million seven hundred and fifty-two thousand t】
Catalog
1. Find single character strchr/strrchr
2. Find any of several characters strpbrk
4. Check two strings one at a time strspc/strcspn
String lookup is divided into : Find a single character in a string , Find multiple characters in a string , Find substring in string .
Find a single character using strchr And strrchr;
Find multiple characters using strpbrk;
Find substrings using strstr;
strspn and strcspn, Check whether the two strings are the same one by one
Like other string handlers , You need to include header files when using these functions <string.h>
1. Find single character strchr/strrchr
strchr And strrchr The function is declared as follows :
char *strchr(const char *s, int c);
char *strrchr(const char *s, intc);Both of these functions are derived from the string s Search for characters c, If you find a character c Just go back to , The return value points to this location , If no characters are found c, return NULL.
strchr And strrchr The difference between functions is :strchr Find characters from left to right ,strrchr Find characters from right to left .
Examples of use , In string str Search for characters c And print the results :
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);The output is as follows :
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. Find any of several characters strpbrk
strpbrk Function declaration :
char *strpbrk(const char *s1, const char *s2);strpbrk Function in the source string s1 Find the first string in the sequence from front to back s2 The position of any character in and returns , It doesn't contain '\0', If a null pointer is not found . The function is defined as follows :
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;
}
}
}Examples of use :
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);
Output results :
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. Find a substring strstr
strstr Function declaration :
char *strstr(const char *str1, const char *str2);strstr Function in string str1 Find from left to right in str2,str1 Continuous inclusion str2 All characters in the , return str1 For the first time str2 A pointer to the position of , If not found , return NULL.
Examples of use :
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);
Output results :
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. Check two strings one at a time strspc/strcspn
strspn and strcspn Function declaration :
size_t strspn(const char *s,const char *accept);
size_t strcspn(const char *s,cosnt char *reject);strspn From a string s The first character of , Check and string one by one accept Whether the characters in inequality , If it's not the same , Stop checking , Returns as a string s The beginning contains accept The number of characters in the ;
strcspn From a string s The first character of , Check and one by one reject Whether the characters in identical , If equal , Stop checking , Returns as a string s The beginning is continuous without a string reject Number of characters in ,
Examples of use :
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);Output results :
str = When we let go of something.it opens up a little space to grow.
bdjk = 0
bdjk = 63
When = 4
When = 0
Use strspn and strcspn Need to pay attention to :
Use strspn when , When the first character is different or the string s It doesn't contain accept When any character in , return 0;
Use strcspn when , When the first character is the same , return 0; When string s It doesn't contain accept Any character of , Return string length .
Reference resources : String lookup function ,C Language string lookup function details (biancheng.net)
边栏推荐
- Mysql 什么是聚集索引和非聚集索引?
- [1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas
- JS anchor positioning can extend many functions
- Mysql之將查詢結果插入到其它錶中
- C language -- 4 first-time constant
- php的exec函数
- Only this is the most true reason why leaders promote you. The rest is nonsense!
- Abbexa acrylamide peg NHS instructions
- Qingniao Changping campus of Peking University: can I learn UI with a high school degree?
- 报错解决Error parsing Mapper XML
猜你喜欢
PHP pseudo protocol implementation command execution details

AI blessing real-time interaction | analysis of zegoavatar facial expression following technology

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

C language -- 8 familiar keywords

As a programmer, is it really that important for the underlying principles?

C use s7 Net connected to Siemens s1200plc, C # directly connected to Siemens PLC
![[qingniaochangping campus of Peking University] the coordinated development of vocational education and general education, will this year's high school entrance examination be easy?](/img/3a/1376593d02adf29a1c37d894dcc0fa.png)
[qingniaochangping campus of Peking University] the coordinated development of vocational education and general education, will this year's high school entrance examination be easy?

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

Factory and strategy mode implementation scheme of coupons

【MySQL】表数据的增删查改(DML)
随机推荐
数组 求上升区间的高度和
Mysql的回表查询?如何避免?
Abbexa 8-OHdG CLIA kit solution
关于type-c
C language ---2 initial knowledge of data types
Summary of common mysql8 commands in centos7 environment
【MySQL】常见数据类型总结
The system needs to scan all the files and try to identify the cover of the video
一次SQL查询优化原理分析(900W+数据从17s到300ms)
What do software test engineers do?
Abbexa 1,3-dipalmitonin CLIA kit solution
PMP candidates, there are these places for PMP examination in Shenzhen in June 2022
【生成对抗网络学习 其一】经典GAN与其存在的问题和相关改进
数组 移动0
Array rotate array
Can I make up the exam if I fail the soft exam? Here comes the answer
Cordova plugin /jpush phonegap Aurora push_ Local push_ Message push
Array remove duplicates from an array
Intelligent robot making progress in imitation learning
Anti shake and throttling