【问题标题】:Word search inside a file c文件内的单词搜索 c
【发布时间】:2013-10-15 04:31:58
【问题描述】:

基本上我已经几乎解决了在文件中查找单词数组的问题,例如:

aaaaaaaaaaaaaaaaaaaaa
setrsdfdsrrtdpyrinoeq
weraderelefantewwerrr
trtevhjujhaspescadito
rtxvfdghhgperrodrdvbh
ifghhfgaaasdserpiente
naendsdsadsasafrrsdft
nssdofgfgghghghdddddd
ttegatovvvfgfyhgggggg
rrrrrrrrrrrrrrrrrrrrr

并输出在其他文件中找到的坐标。

但是我不知道如何搜索垂直词,我做的代码是逐行搜索一个词,如果我找到它就重新调整它的坐标。

我知道我必须实现其他功能来搜索垂直词,但最好的方法是什么?

如何改变what_coor函数来搜索垂直词? 所以它可以找到例如单词“ant”

aaaaaaaaaaaaaaaaa*a*aaa
bbbbbbbbbbbbbbbbb*n*bbb
rrrrrrrrrrrrrrrrr*t*rtr

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define MAX_OF_LINE 180 //max length of a line 

//search a word in a line if is there returns coordinate
int what_coor ( const char *line, const char *word ){
    const char *p, *x,*y;
    for ( p = line; *p != '\0'; p++ ) {
        x = p;
        y = word;
        for ( ; *x != '\0' && *y != '\0' && *x == *y; x++, y++ )
            ;
            if(*y == '\0')
                  return p - line + 1;
    }
    return -1;
}

//reservamos espacio para nuestro myArray 'bidimensional'
char **askMemory(int filas, int cols){
   char **myArray;
   int i;
   /* espacio para el myArray de apuntadores a entero*/
   myArray = (char**)malloc(filas * sizeof(char*));
   if(myArray == NULL){
      fprintf(stderr, "Error 1-d \n");
      exit(-1);
   }                                                                               
   /* espacio para los myArrays de letras */
   for(i = 0; i < filas; i++){
      myArray[i] = (char*)malloc(cols * sizeof(char));
      if(myArray[i] == NULL){
         fprintf(stderr, "Error 2-d\n");
         exit(-1);
      }
   }                                                                               
   return myArray;
}
//returns bidimensional array, filling it with a file
char **read (int *ptrNumWords, char *fileName) {
  char line[MAX_OF_LINE];   
  char **myArray;
  int i;
  FILE *f;
  if ( ! ( f = fopen (fileName, "r") ) ) {
    printf ("error opening %s\n", fileName);
    exit (-1);
  }
   while ( fgets (line, MAX_OF_LINE, f) ) {
         *ptrNumWords = *ptrNumWords+1;
  }
  myArray = askMemory (*ptrNumWords, MAX_OF_LINE);
  rewind (f);
  for ( i=0 ; i <*ptrNumWords ; i++ ) {
    fgets (myArray[i], MAX_OF_LINE, f);
    myArray[i][strlen(myArray[i])] = '\0';
  }
  fclose (f);
  return (myArray);
}

//seacrh word and writes output
void searchForIt(int rows, char **array2d , char *target){
     FILE *sal;
     sal = fopen("out.txt","a+");     
     int i, col;
     for ( i = 0; i < rows; i++ ) {
         col =  what_coor ( array2d[i], target );
         if( col != -1 ){
             fprintf(sal,"\"%s\" \t found on (%d,%d)\n",target, i+1, col);             
             printf ( "\"%s\" \t found on (%d,%d)\n", target, i+1, col );            
         }    
     }
      fclose(sal); 
}


int main (){
  int ptrNumWords = 0;
  int i=0;
  char **array2d;
  array2d  = read(&ptrNumWords, "in.txt"); 
  char *wordsBusca[] = {"gato", "perro", "raton", "elefante", "rino", "serpiente", "pescadito"};
  int len = sizeof(wordsBusca) / sizeof(char *) ;
  for(i =0; i< len; i++){
    searchForIt(ptrNumWords,array2d, wordsBusca[i] );
  }
  getch();
  return 0;
}

【问题讨论】:

  • 您可以将它们读入二维字符数组,然后使用 Knuth–Morris–Pratt 算法进行水平和垂直扫描。

标签: c matrix


【解决方案1】:

您可以执行以下操作:

总结:

第 1 步:从 2D 矩阵中的文件中读取数据。矩阵的每个元素都是char
第 2 步:对矩阵进行转置。
第 3 步:使用您的函数 my_coor 水平搜索单词。

详情:
您可以通过获取每行的字符数和行数来执行步骤 1,然后动态分配 2D char 矩阵空间。然后逐个字符地读取字符并继续将每个字符保存在矩阵中的正确位置。
第 2 步需要基本的数学和交换。
第 3 步你已经实现了。

【讨论】:

    【解决方案2】:

    我不久前实施了@0xF1 建议的变体以回答另一个问题:请参阅我的解决方案here。在此,我实际上将 10x10 矩阵变成了 10x40 矩阵,矩阵中的连续行对应于原始 10x10 中的一行,从左到右、从右到左、从上到下、从下到上读取。

    这种方法(或更简单的 10x20 - 仅从左到右和从上到下)的优点是您现在可以使用所有可能的方向进行快速搜索 - 您可以将每个单词与 40(或 20)行匹配,而无需在此期间进行任何转置等。

    我认为这是一个有用的改进。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多