统计一个长度替2的子字符串在另一个字符串中出现的次数.例如:假定输入的字符串为“asd asasdfg asd as zx67 asd mklo”,子字符串为“as”,函数返回值为6
统计一个长度为2的子字符串在另一个字符串中出现的次数.例如:假定输入的字符串为“asd asasdfg asd as zx67 asd mklo”,子字符串为“as”,函数返回值为6。
编写一个函数findStr(),该函数统计一个长度为2的子字符串在另一个字符串中出现的次数。例如:假定输入的字符串为“asd asasdfg asd as zx67 asd mklo”,子字符串为“as”,函数返回值为6。
函数readwriteDat()的功能是实现从文件IN90.DAT中读取两个字符串,并调用函数findStr(),最后把结果输出到文件OUT90.DAT中。
编写一个函数findStr(),该函数统计一个长度为2的子字符串在另一个字符串中出现的次数。例如:假定输入的字符串为“asd asasdfg asd as zx67 asd mklo”,子字符串为“as”,函数返回值为6。
函数readwriteDat()的功能是实现从文件IN90.DAT中读取两个字符串,并调用函数findStr(),最后把结果输出到文件OUT90.DAT中。
#include <stdio.h> #include <string.h> #include <windows.h> #include <conio.h> #define N 81 int findStr(char *str,char *substr) { int i,j=0; for(i=0;((i<81)&&(str[i+1]!='\0'));i++) { if((str[i]==substr[0])&&(str[i+1]==substr[1])) {j++;i++;} } return j; } main() { char str[81],substr[3]; int n; system("cls"); printf("Enter a string : "); gets(str); printf("Enter a substring: "); gets(substr); puts(str);puts(substr); n=findStr(str,substr); printf("n=%d\n",n); ReadWrite(); } ReadWrite() { char str[81],substr[6],ch; int n,len,i=0; FILE *rf, *wf ; rf = fopen("IN90.DAT", "r") ; wf = fopen("OUT90.DAT", "w") ; while(i<5) { fgets(str,80,rf); fgets(substr,10,rf); len=strlen(substr)-1; ch=substr[len]; if(ch=='\n'||ch==0x1a) substr[len]=0; substr[2]=0; n=findStr(str,substr); fprintf(wf,"%d\n",n); i++; } fclose(rf); fclose(wf); }
- 1楼jianxia_wzx昨天 23:59
- 长度只是2体现不出算法的特点。如果字串长度为m,源串为n,m<n。这样你的算法咋写?