字符串匹配,但是无法输入子串,请问问题出在哪里?
程序代码:
#include<stdio.h> #include<malloc.h> #include<string.h> #define string_size 10000 int T_len; int S_len; typedef struct LinkSeq { char str[string_size]; int length; }Seq; int intgNum = 0,charNum = 0,blankNum = 0; void Statistics(Seq Essay) { int j=0; while(Essay.str[j]!='\0') { if( ('A'<=Essay.str[j]&&Essay.str[j]<='Z') || ('a'<=Essay.str[j]&&Essay.str[j]<='z')) charNum ++; else if( '0'<=Essay.str[j]&&Essay.str[j]<='9') { puts("its a integer!"); getch(); intgNum ++; } else if(Essay.str[j]==' ') { puts("its a blank space!"); getch(); blankNum ++; } } } void GetNext(char T[],int *next) { int i,j; i = 1; j = 0; next[1] = 0; while(i < T_len) { if(j==0 || T[i] == T[j]) { ++i; ++j; next[i] = j; } else j = next[j]; } } int Index_KMP(char S[],char T[],int pos) { int i = pos; int j = 1; int next[255]; GetNext(T,next); while(i <= S_len && j <= T_len) { if(j==0 || S[i] == T[j]) { ++i; ++j; } else j = next[j]; } pos = i; if(j > T_len) return i-T_len; else return 0; } void SubStrFreq(Seq Essay,char T[]) { //int times = 0; int startLoc; startLoc = Index_KMP(Essay.str,T,0); printf("we find the substr at %d \n",startLoc); //return times; } /* void DelSubStr(Seq Essay) { } */ void main() { int i,j,row,N_line; Seq Essay; char subStr[string_size]; FILE *fp; if( (fp=fopen("A.txt","r")) == NULL) { puts("Cannot open the file!"); getch(); exit(0); } printf("please input lines of the essay: "); scanf("%d",&N_line); for(row=1,j=0; row<N_line; row++) for(i=0; i<80; i++,j++) { Essay.str[j] = fgetc(fp); putchar(Essay.str[j]); } Essay.str[j++] = '\0'; Essay.length = j; // 记录字符总数 // printf("\nthe total number of character is %d:\nthe total number of blank space is %d:\nthe total number of words is %d:\n",charNum,blankNum,Essay.length); printf("please input the string you search:"); gets(subStr); //这句无法执行 T_len = strlen(subStr); S_len = strlen(Essay.str); SubStrFreq(Essay,subStr); getch(); fclose(fp); }
A.rar
(3.82 KB)