Linux 拨号vps windows公众号手机端

c语言strstr函数的作用是什么

lewis 8年前 (2017-01-08) 阅读数 7 #程序编程
文章标签 c语言

在C语言中,strstr函数用于在一个字符串中查找另一个字符串第一次出现的位置。该函数的原型如下:

char *strstr(const char *haystack, const char *needle);

其中,haystack是要查找的字符串,needle是要查找的子字符串。该函数会返回一个指向第一次出现needle的位置的指针,如果未找到则返回NULL。

例如,如果有字符串"hello world",要查找其中是否包含字符串"world",可以使用strstr函数:

char *result = strstr("hello world", "world");
if (result != NULL) {
    printf("Found at position: %ld\n", result - "hello world");
}

上述代码会输出Found at position: 6,表示在字符串"hello world"中找到了子字符串"world",并且是从位置6开始的。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门