当前位置:网站首页>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
边栏推荐
- Slide the custom control to close the activity control
- 51nod1277 maximum value in string [KMP]
- SeaweedFS安全配置(Security Configuration)
- Symphony tutorial
- Signal works: time varying and time invariant
- JVM instructions for four call methods
- std::unique_ptr<T>与boost::scoped_ptr<T>的特殊性
- 子串分值-超详细版——最后的编程挑战
- 点在多边形内外的判断
- 這個開源項目超哇塞,手寫照片在線生成
猜你喜欢

Using rancher to build kubernetes cluster

MySQL innodb每行数据长度的限制

Alibaba cloud firewall configuration, multiple settings (iptables and firewall)

Codeforces Round #645 (Div. 2)

Beautiful ruins around Kiev -- a safe guide to Chernobyl!

nacos注册中心集群

点在多边形内外的判断

Judgment of points inside and outside polygon

自定义控件之侧滑关闭 Activity 控件

Rikka with cake (segment tree + segment tree)
随机推荐
2019.11.3学习总结
2019.10.6训练总结
qgis制图
Slide the custom control to close the activity control
DevExpress的双击获取单元格数据
Wandering -- the last programming challenge
任务调度器之Azkaban的使用
Memory layout of JVM objects
Signal works: time varying and time invariant
在VMware workstation中安装WMware ESXi 6.5.0并进行配置
If I were in Beijing, where would it be better to open an account? In addition, is it safe to open an account online now?
Shanke's C language 2018 exercise (Telecom)
520 diamond Championship 2021
Weight recursion of complete binary tree -- the last programming challenge
1021 Deepest Root (25 分)
Recurrence of vulnerability analysis for Cisco ASA, FTD and hyperflex HX
L2-026 小字辈 (25 分)
To 3 -- the last programming challenge
2019.11.13 training summary
HDU 4578 transformation (segment tree + skillful lazy tag placement)