当前位置:网站首页>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
边栏推荐
猜你喜欢

基辅周边的凄美废墟——切尔诺贝利的安全前往指南!

时变和非时变

Dynamic linking of virtual machine stack of JVM

Recurrence of vulnerability analysis for Cisco ASA, FTD and hyperflex HX

Judgment of points inside and outside polygon

Recyclerview sticky (suspended) head

Dev使用过程中的基本操作

图片验证码控件

使用Rancher搭建Kubernetes集群

Using rancher to build kubernetes cluster
随机推荐
函数指针、函数指针数组、计算器+转移表等归纳总结
Reverse thinking - short story
Ce projet Open source est super wow, des photos manuscrites sont générées en ligne
点在多边形内外的判断
2019.10.16 training summary
std::unique_ptr<T>与boost::scoped_ptr<T>的特殊性
2019.10.6训练总结
蛇形填数
QGIS mapping
2019.10.27训练总结
指针数组、数组指针和传参的相关问题
Shanke's C language 2018 exercise (Telecom)
2019.11.17训练总结
The Stones Game【取石子博弈 & 思维】
Codeforces Round #645 (Div. 2)
2019.10.20训练总结
Minorgc, majorgc, fullgc
Codeforces Round #659 (Div. 2)
L2-3 这是二叉搜索树吗?-题解超精彩哦
Picture verification code control