#include <conio.h> #include <stdio.h> #include <string.h> #include <stdlib.h> int fun(char *s, char *t)//寻找函数 { int n; char *p, *r; n = 0; while(*s) { p = s; r = t; while(*r)//如果不为空一直循环直到结束 if(*r == *p) { r++; p++; } else//不相等退出 break; if(*r == '\0') n++; s++; } return n; } main( ) { char s[100], t[100]; int m; system("cls"); printf("\nPlease enter string S:" );//输入母字符串 scanf("%s", s); printf("\nPlease enter substring t:");//输入子字符串 scanf("%s", t); m = fun(s, t); printf("\nThe result is : m = %d\n", m); }
*r=='\0'是判断r指针指向的字符是不是0,如果是0就意味着字符串结束
r是指针(或者地址),'\0'是字符,指针不能与字符进行比较