检查子存在于在C字符串 [英] check substring exists in a string in C

查看:85
本文介绍了检查子存在于在C字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个字符串是否包含类似于C的子字符串:

 的char *发送=这是我的样本榜样;
字符*字=样本;
如果(/ *句子包含文字* /){
    / * .. * /
}

所以基本上我所要求的东西,而不是使用字符串::找到在C ++


解决方案

 如果(的strstr(发送字)!= NULL){
    / * ... * /
}

注意的strstr 在返回一个指针字的开头发送如果字中找到。

I'm trying to check whether a string contains a substring in C like:

char *sent = "this is my sample example";
char *word = "sample";
if (/* sentence contains word */) {
    /* .. */
}

So basically I'm asking for something to use instead of string::find in C++

解决方案

if(strstr(sent, word) != NULL) {
    /* ... */
}

Note that strstr returns a pointer to the start of the word in sent if the word word is found.

这篇关于检查子存在于在C字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆