C找到字典中的所有字谜

时间:2015-11-27 10:48:34

标签: c

我正在尝试编写一个程序,在字典中查找所有字谜(通过重新排列字母重新编写的单词)(Linux中的/ usr / share / dict / words文件)。字典文件包含许多以"结尾的单词。 ' s"我想从检查中排除。这是我写的那样做的,但不幸的是结果文件包含只有一个字母的行" s"我不知道它来自哪里。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define TRUE    1
#define FALSE   0

int wordContainsNonAlpha(char *word);

int main(int argc, const char *argv[]){
   FILE *fp_read = fopen("/usr/share/dict/words", "r");
   char *word = malloc(sizeof(word));
   FILE *fp_write = fopen("words.txt","w");

   while (fgets(word,sizeof(word), fp_read) != NULL){
      if (wordContainsNonAlpha(word)){
         fprintf(fp_write,"%s","\n");
      }
      else{
//         fputs(word, stdout);
         fprintf(fp_write,"%s",word);
        }
   }

   fclose(fp_read);
   fclose(fp_write);

   return 0;
}

int wordContainsNonAlpha(char *word){
   int currentLetter = 0;
   int wordLenght = strlen(word);
   int result = FALSE;
   char ch;

   while ( (currentLetter < wordLenght) && (result == FALSE) ){
      ch = word[currentLetter];
      if (ch == '\''){
//      if (!isalpha(ch)){
         result = TRUE;
         break;
      }
      currentLetter++;
   }

   return result;
}

结果是:

    $ sdiff words.txt /usr/share/dict/words | more
    A                                                               A
                                                                  | A's
                                                                  | AA's
                                                                  | AB's
                                                                  | ABM's
                                                                  | AC's
                                                                  | ACTH's
                                                                  | AI's
                                                                  | AIDS's
                                                                  | AM's
    AOL                                                             AOL
                                                                  | AOL's
                                                                  | ASCII's
                                                                  | ASL's
                                                                  | ATM's
                                                                  | ATP's
                                                                  | AWOL's
                                                                  | AZ's
                                                                  | AZT's
                                                                  <
    Aachen                                                          Aachen
    Aaliyah                                                         Aaliyah
    Aaliyah                                                       | Aaliyah's
    Aaron                                                           Aaron
    Abbas                                                           Abbas
    Abbasid                                                         Abbasid
    Abbott                                                          Abbott
                                                                  | Abbott's
    s                                                             <
    Abby                                                            Abby
                                                                  | Abby's
    Abdul                                                           Abdul
                                                                  | Abdul's
                                                                  <
    Abe                                                             Abe
                                                                  | Abe's
    Abel                                                            Abel
........

如果我尝试使用函数isalpha,结果会更糟,因为它似乎正在寻找具有特定长度的单词并且根本不正确:

sdiff words.txt /usr/share/dict/words | more
                                                              | A
                                                              | A's
                                                              | AA's
                                                              | AB's
                                                              | ABM's
                                                              | AC's
                                                              | ACTH's
                                                              | AI's
                                                              | AIDS's
                                                              | AM's
                                                              | AOL
                                                              | AOL's
                                                              | ASCII's
                                                              | ASL's
                                                              | ATM's
                                                              | ATP's
                                                              | AWOL's
                                                              | AZ's
                                                              | AZT's
                                                              | Aachen
                                                              <
Aaliyah                                                         Aaliyah
Aaliyah                                                       | Aaliyah's
                                                              | Aaron
                                                              | Abbas
Abbasid                                                         Abbasid
                                                              | Abbott
                                                              | Abbott's
                                                              | Abby
                                                              | Abby's
                                                              | Abdul
                                                              | Abdul's
                                                              | Abe
                                                              | Abe's
                                                              | Abel
                                                              | Abel's
                                                              <
                                                              <
Abelard                                                         Abelard
Abelson                                                         Abelson
Abelson                                                       | Abelson's
Aberdee                                                       | Aberdeen
Aberdee                                                       | Aberdeen's
Abernat                                                       | Abernathy
Abernat                                                       | Abernathy's
Abidjan                                                       <
Abidjan                                                         Abidjan

你能帮忙吗?

1 个答案:

答案 0 :(得分:2)

您的问题来自您对malloc()的调用:

char *word = malloc(sizeof(word));
// Here, you allocate sizeof(char*) bytes which is only the size of a pointer and not the size of a dictionary word


while (fgets(word,sizeof(word), fp_read) != NULL){
// In this case, fgets does not stop when you expect it

要解决这个问题,你可以简单地为你的分配使用一个常量,它是字典中最长单词的长度或任意值(我用64快速测试)

关于isalpha()的问题,这是因为fgets()会在你的单词中存储'\ n'。

来自man fgets:

  

如果读取换行符,则将其存储到缓冲区

所以,你可以使用:

    if (ch != '\n' && !isalpha(ch)){