實現對以下英文文本的關鍵詞尋找。
支援如下形式調用:search.exe input.txt keyword
輸出:關鍵詞首次出現位置
若實現萬用字元尋找有加分(若實現了在文檔中說明)
1 #include "stdio.h" 2 #include "string.h" 3 #include "stdlib.h" 4 #include "conio.h" 5 6 void main(int argc,char **argv) 7 { 8 int MyCount; //檔案字元總數 9 int i=0,j;10 int m=0,n=0; //行數m,每行的數目n11 FILE *fp;12 char word[5000]; //儲存文本字元數組13 14 char str[20]; //關鍵字15 bool flag; //尋找標誌16 17 char *Keyword;18 char *input;19 if(argc<3){20 input="input.txt";21 Keyword="1234";22 23 }else{24 input=*(argv+1);25 Keyword=*(argv+2);26 }27 28 if((fp=fopen("input.txt","r"))==NULL)29 {30 printf("cannot open file.\n");31 exit(0);32 }33 while(!feof(fp)) //讀寫字元34 {35 word[i]=fgetc(fp);36 putchar(word[i]);37 i++;38 39 }40 MyCount=i;41 42 printf("\n請輸入關鍵字 :");//輸入關鍵字43 scanf("%s",str);44 for(i=0;i<MyCount;i++,n++)45 {46 flag=true;47 48 if(word[i]=='\n')49 {50 m++;51 n=0;52 }53 for(j=0;str[j]!='\0';j++)//匹配54 if(str[j]!=word[i+j])55 { flag=false;56 break;57 }58 if(flag==true)59 {60 printf("\n關鍵字 %s 在文本裡面 %d,第 %d 個 字串.\n",str,m,++n);61 break;62 } 63 }64 getch();65 66 }