C语言 strstr

C语言 strstr

#include <string.h>
char *strstr(const char *haystack, const char *needle);

功能:在字符串haystack中查找字符串needle出现的位置
参数:

  • haystack:源字符串首地址
  • needle:匹配字符串首地址

返回值:

  • 成功:返回第一次出现的needle地址
  • 失败:NULL

案例

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
    char ch[] = "hello world";
    char str[] = "llo";
    // 查找字符串
    char* p = strstr(ch, str);
    // 记录出现位置
    printf("%s\n", p);

    return 0;
}
strstr

 

posted @ 2020-02-28 19:01  kevin.Xiang  阅读(769)  评论(0编辑  收藏  举报