当前位置:网站首页>C语言库函数--strstr()
C语言库函数--strstr()
2022-06-29 09:26:00 【一缕阳光a】
原型:extern char *strstr(const char *str1, const char *str2);
需要包含的头文件:#include <string.h>
作用:用于判断字符串str2在字符串str1中第一次出现的位置。如果没有找到则返回NULL,找到了则返回str1中的位置。
下面的代码为例:
ptr_zlib指向"zlib;compression=lz4"的首字符'z'
#include <iostream>
#include <string>
#include <string.h>
int main ()
{
const char *comment = "compression=zlib;compression=lz4";
const char *ptr_zlib = strstr(comment, "zlib");
const char *ptr_lz4 = strstr(comment, "lz4");
if ((NULL != ptr_zlib) && (NULL != ptr_lz4)) {
std::cout << "double compression" << std::endl;
}
return 0;
}
结果:double compression但是这个函数的时间复杂度比较高O(M*N),建议使用KMP算法进行查找,参考下面的这篇文章:
不过这个方法返回的是一个下标。找到了返回子串在父串中第一个位置的下标,找不到返回-1
边栏推荐
- Download control 1 of custom control (downloadview1)
- 51nod1277 maximum value in string [KMP]
- HDU 4578 transformation (segment tree + skillful lazy tag placement)
- 基辅周边的凄美废墟——切尔诺贝利的安全前往指南!
- mysql 8.0 一条insert语句的具体执行流程分析(二)
- 1146 Topological Order (25 分)
- Use of Azkaban in task scheduler
- 520 diamond Championship 2021
- Rikka with Cake(线段树+线段树)
- 逆向思维-小故事
猜你喜欢

TLAB of JVM

Using rancher to build kubernetes cluster

Codeforces Round #659 (Div. 2)

Use of Azkaban in task scheduler

EDA与VHDL题库

HDU 6778 car (group enumeration -- > shape pressure DP)

HDU 6778 Car (分组枚举-->状压 dp)

Text of the basic component of the shutter

520 钻石争霸赛 2021

Download control 1 of custom control (downloadview1)
随机推荐
十六制计数器和流水灯
2019.11.13训练总结
Arc view and arc viewpager
2019.11.17 training summary
2019.11.20 training summary
1146 Topological Order (25 分)
Weight recursion of complete binary tree -- the last programming challenge
Nacos environmental isolation
sympy的dsolve函数
Codeforces Round #641 Div2
std::unique_ptr<T>与boost::scoped_ptr<T>的特殊性
51nod1277 字符串中的最大值【KMP】
时变和非时变
1147 heaps (30 points)
Binding mechanism of JVM methods
Analyze in detail the PBOT mining virus family behavior and the principle of exploited vulnerabilities, and provide detailed protection suggestions for the blue army
Summary after the 2009 ICPC Shanghai regional competition
SymPy Tutorial(译)
Talk about threads and concurrency
信号作品:时变和时不变