C 在字典里找出所有的字谜
我正试图编写一个程序,在字典(/usr/share/dict/words file in Linux)中查找所有的字谜(通过重新排列字母重新生成的单词)。字典文件包含许多以“'s”结尾的单词,我想从检查中排除这些单词。我就是这样写的,但不幸的是,结果文件只包含一个字母“s”的行,我不知道它来自哪里C 在字典里找出所有的字谜,c,C,我正试图编写一个程序,在字典(/usr/share/dict/words file in Linux)中查找所有的字谜(通过重新排列字母重新生成的单词)。字典文件包含许多以“'s”结尾的单词,我想从检查中排除这些单词。我就是这样写的,但不幸的是,结果文件只包含一个字母“s”的行,我不知道它来自哪里 #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h>
#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;
}
#包括
#包括
#包括
#包括
#定义真1
#定义FALSE 0
int-wordContainsNonAlpha(字符*单词);
int main(int argc,const char*argv[]{
文件*fp_read=fopen(“/usr/share/dict/words”,“r”);
char*word=malloc(sizeof(word));
文件*fp_write=fopen(“words.txt”,“w”);
while(fgets(word,sizeof(word),fp_read)!=NULL){
if(单词containsnonalpha(单词)){
fprintf(fp_write,“%s”,“\n”);
}
否则{
//FPUT(字、标准输出);
fprintf(fp_write,“%s”,word);
}
}
fclose(fp_read);
fclose(fp_写入);
返回0;
}
int-wordContainsNonAlpha(字符*word){
int currentLetter=0;
int-wordLenght=strlen(单词);
int结果=假;
char ch;
while((currentLetter
结果是:
$ 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
........
$sdiff words.txt/usr/share/dict/words |更多
A
|A的
|机管局
|AB's
|ABM's
|空调
|ACTH's
|人工智能
|艾滋病
|AM的
美国在线
|美国在线
|ASCII的
|ASL's
|自动取款机
|ATP的
|擅离职守
|阿兹
|AZT's
<
亚琛亚琛
啊,啊,啊
阿利耶|阿利耶的
亚伦
阿巴斯
阿巴斯
雅培雅培
|雅培
<
艾比艾比
|艾比的
阿卜杜勒·阿卜杜勒
|阿卜杜勒氏
<
安倍
|安倍的
亚伯亚伯
........
如果我尝试使用函数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
sdiff words.txt/usr/share/dict/words |更多
|A
|A的
|机管局
|AB's
|ABM's
|空调
|ACTH's
|人工智能
|艾滋病
|AM的
|美国在线
|美国在线
|ASCII的
|ASL's
|自动取款机
|ATP的
|擅离职守
|阿兹
|AZT's
|亚琛
<
啊,啊,啊
阿利耶
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
if (ch != '\n' && !isalpha(ch)){