题解:
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int fun(char *str, char *substr)
{
int i,j=0;
for(i=0;str[i+1]!='\0';i++) /*如果一个长度为2的子字符串在主字符串中出现一次,则j+1,如此循环*/
if(str[i]==substr[0]&&str[i+1]==substr[1])
j++;
return j; /*返回子字符串在主字符串中出现的次数*/
}
void main()
{
FILE *wf;
char str[81],substr[3];
int n;
system("CLS");
printf("输入主字符串: ");
gets(str);
printf("输入子字符串: ");
gets(substr);
puts(str);
puts(substr);
n=fun(str,substr);
printf("n=%d\n ",n);
/******************************/
wf=fopen("out.dat","w");
n=fun("asd asasdfg asd as zx67 asd mklo","as");
fprintf(wf,"%d",n);
fclose(wf);
/*****************************/
}