头文件:
?
|
1
|
#include <include.h>
|
strpbrk()函数检索两个字符串中首个相同字符的位置,其原型为:
?
|
1
|
char *strpbrk( char *s1, char *s2);
|
【参数说明】s1、s2要检索的两个字符串。
strpbrk()从s1的第一个字符向后检索,直到'\\0',如果当前字符存在于s2中,那么返回当前字符的地址,并停止检索。
【返回值】如果s1、s2含有相同的字符,那么返回指向s1中第一个相同字符的指针,否则返回NULL。
注意:strpbrk()不会对结束符'\\0'进行检索。
【函数示例】输出第一个相同字符之后的内容。
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include<stdio.h>
#include<string.h>
int main(void){
char* s1 = "http://see.xidian.edu.cn/cpp/u/xitong/";
char* s2 = "see";
char* p = strpbrk(s1,s2);
if(p){
printf("The result is: %s\\n",p);
}else{
printf("Sorry!\\n");
}
return 0;
}
|
输出结果:
?
|
1
|
The result is: see.xidian.edu.cn/cpp/u/xitong/
|
DEMO:实现自己的strpbrk函数
?
|
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
30
31
32
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#pragma warning (disable:4996)
char *mystrpbrk(const char *cs,const char *ct);
int main(void)
{
char *s1="Welcome to Beijing.";
char *s2="BIT";
char *s3;
s3=mystrpbrk(s1,s2);
printf("%s\\n",s3);
getch();
return 0;
}
/*FROM 百科*/
char *mystrpbrk(const char *cs,const char *ct)
{
const char *sc1,*sc2;
for (sc1=cs;*sc1!='\\0';sc1++)
{
for (sc2=ct;*sc2!='\\0';sc2++)
{
if (*sc1==*sc2)
{
return (char *)sc1;
}
}
}
return NULL;
}
|
?
|
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
30
31
32
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#pragma warning (disable:4996)
int main(void)
{
char *s1="Welcome to Beijing.";
char *s2="BIT";
char *p;
system("cls");
p=strpbrk(s1,s2);
if (p)
{
printf("%s\\n",p);
}
else
{
printf("NOT Found\\n");
}
p=strpbrk(s1,"i");
if (p)
{
printf("%s\\n",p);
}
else
{
printf("NOT Found\\n");
}
getch();
return 0;
}
|
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 20
-
2025-05-27 17
-
2025-05-29 90
-
2025-06-04 101
热门评论

