学什么讲什么--C标准函数 strstr

[复制链接]
查看657 | 回复7 | 2023-10-17 09:44:16 | 显示全部楼层 |阅读模式
本帖最后由 ckdsx.cn 于 2023-10-17 10:38 编辑

[size=1.8em]描述
C 库函数 char *strstr(const char *haystack, const char *needle) 在字符串 haystack 中查找第一次出现字符串 needle 的位置,不包含终止符 '\0'。
声明
下面是 strstr() 函数的声明。
char *strstr(const char *haystack, const char *needle)参数
  • haystack -- 要被检索的 C 字符串。
  • needle -- 在 haystack 字符串内要搜索的小字符串。
返回值
该函数返回在 haystack 中第一次出现 needle 字符串的位置,如果未找到则返回 null。
实例
下面的实例演示了 strstr() 函数的用法。
实例#include <stdio.h>#include <string.h>  int main(){   const char haystack[20 = "RUNOOB";   const char needle[10 = "NOOB";   char *ret;    ret = strstr(haystack, needle);    printf("子字符串是: %s\n", ret);      return(0);}


让我们编译并运行上面的程序,这将产生以下结果:
子字符串是: NOOB------------以上内容来自网络:strstr 函数使用需要引用头文件 string.h在电脑上使用C语言编译器测试,返回值RET,返回的是指针首地址,并非是字符串第一次出现位置,求教是何原因?

屏幕截图 2023-10-17 100539.png
测试代码如下:

#include <stdio.h>
#include <string.h>

int main()
{
    const char haystack[20] = "runnoob";
    const char needle[10] = "noob";
    char *ret=0;
     //   ret = strstr(haystack, needle);
    if(strstr(haystack,needle)){
    printf("child string is : %d\n", strstr(haystack,needle));
    printf("child string is : %s\n", strstr(haystack,needle));
    }
    else
    printf("code is null");

    return (0);
}

那位有时有闲可以帮忙测试验证过一下,谢谢!



回复

使用道具 举报

496199544 | 2023-10-17 10:00:52 | 显示全部楼层
学习
回复

使用道具 举报

WangChong | 2023-10-17 10:26:22 | 显示全部楼层
学习了
回复

使用道具 举报

bzhou830 | 2023-10-17 10:47:53 | 显示全部楼层
学习了
选择去发光,而不是被照亮
回复

使用道具 举报

爱笑 | 2023-10-17 10:56:12 | 显示全部楼层
学习了
用心做好保姆工作
回复

使用道具 举报

linyuuki | 2023-11-17 22:30:17 | 显示全部楼层
好像看到了菜鸟站的内容。
回复 支持 反对

使用道具 举报

心云 | 2023-12-18 09:56:21 | 显示全部楼层
回复

使用道具 举报

Sssnnn | 2024-4-8 19:04:12 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则