这是一个在一个字符串中统计出另一个子串出现的次数,并返回次数n,保存在文件out.dat中。 #include<stdio.h> Void ReadWrite(char *str1,char *str2) {FILE *fp; fp=fopen("C:\\out.dat","w+"); if(fp) {if(*str1) {fprintf(fp,"%s",*str1); str1++; } if(*str2) {fprintf(fp,%s",*str2); str2++; } } else printf("Can't find or something is wrong to the file!"); } int findstr(char *str1,char *str2) {char *p,*r; int n=0; while(*str1) {p=str1; r=str2; while(*r) {if(*r==*p) {r++; p++; } else break; if(*r=='\0') {n++; str1++; } } return n; } main() {char *str1,str2; gets(str1); gets(str2); printf("%d,findstr(str1,str2)); ReadWrite(str1,str2); }

评论