我是C的新手,我很乐意为这个程序提供任何帮助:
任务:
用户将输入4-7个字母(例如“ADFG”) .
我有分离的文本文件,其中包含大约几千个单词
(例如:
-
BDF
-
BGFK
-
JKLI
-
NGJKL
-
POIUE
等等 . )
- 没有那个标记的列表中写的
我想制作程序,它从这个文本文件中找到与用户输入的字母相同的单词(在这种情况下,当我输入ADFG时,它将找到并显示BDF,BGFK,NGJKL) .
到目前为止这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char enter[8],row[80];
printf("4-7 different letteres: ");
gets(enter);
if (strlen(enter)>7 || strlen(enter)<4)
{
puts("incorrect number of letters");
return 1;
}
typedef struct structure
{
char row[50];
}Row;
Row textrow[40000];
FILE *file;
file = fopen("words.txt","r");
if (file==NULL)
{
printf("Error! File %s can not be opened.","words.txt");
return 1;
}
int i=0;
char words[30];
while (!feof(file))
{
fscanf(file,"%s",&textrow[i].row[0]);
for(int j=0;j<strlen(enter);j++)
{
for(int k=0;k<strlen(textrow[i].row);k++)
{
words=strchr(textrow[i].row[k],enter[j]);
printf("%s",words);
}
}
i++;
}
fclose(file);
return 0;
}
谢谢你的帮助 .
2 回答
正如其他人所评论的那样,鉴于您的代码可能能够按原样编译和构建,您的问题在于该方法,您需要确定完成所需内容所需的步骤 . 创建算法 . 以下是执行我认为您想要执行的操作的一些步骤:
Problem statement: 如果文本文件包含许多单词,并且用户输入了1个或多个字符,请查找文件中包含用户输入的一个或多个字符的所有单词 .
Steps: (进近)
1) 将文本文件的所有单词读入字符串数组
2) 将用户输入字符串输入char数组
3) In循环:检查每个字符串的每个字符串的用户输入的每个字符
例如使用
strpbrk