使用strstr()函数在一个字符串中查找另外一个字符串

#include <stdio.h>
#include <string.h>
int main(void){
  char *loc, buf1[80], buf2[80];
  
  //输入字符串
  printf("Enter the string to be searched: ");
  gets(buf1);
  printf("Enter the target string: ");
  gets(buf2);
  
  //执行查找
  loc = strstr(buf1, buf2);
  if(loc == NULL){
    printf("No match was found.\n");
  } else {
    printf("%s was found at positioin %d.\n", buf2, loc-buf1);
  }
  return 0;
}
相关文章
相关标签/搜索