Skip to content

Latest commit

 

History

History
53 lines (37 loc) · 1.81 KB

53.md

File metadata and controls

53 lines (37 loc) · 1.81 KB

C strstr()函数

原文: https://beginnersbook.com/2017/11/c-strstr-function/

strstr()函数在指定主字符串中搜索给定字符串,并返回指向给定字符串第一次出现的指针。

C strstr()函数声明

char *strstr(const char *str, const char *searchString)

str - 要搜索的字符串。

searchString - 我们需要在其中搜索字符串str的字符串

strstr()的返回值

此函数返回指向给定字符串第一次出现的指针,这意味着如果我们打印此函数的返回值,它应显示主字符串的一部分,从给定字符串开始直到主字符串末尾。

示例:C 中的strstr()函数

#include <stdio.h>
#include <string.h>
int main () {
   const char str[20] = "Hello, how are you?";
   const char searchString[10] = "you";
   char *result;

   /* This function returns the pointer of the first occurrence
    * of the given string (i.e. searchString) 
    */ 
   result = strstr(str, searchString);
   printf("The substring starting from the given string: %s", result);
   return 0;
}

输出:

The substring starting from the given string: you?

正如您所看到的那样,我们正在使用函数strstr()在字符串"Hello, how are you?"中搜索字符串"you"。由于函数将返回字符串"you"的第一次出现的指针,因此字符串str的从"you"开始的子字符串已被打印为输出。

相关文章:

  1. C - strspn()示例
  2. C - strrchr()示例
  3. C - strcpy()示例
  4. C - strcmp()示例