#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);}
就是输入一个字符串,再输入一个子字符串,判断是否相等
#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);
}