我是C的新手,我很高兴在这个项目上有任何帮助:
任务:
用户将输入4-7个字母(例如'ADFG')。
我已经分离了包含几千个单词的文本文件。
(例如:
等)
在列表中没有标记的-its
我想要制作程序,它从这个文本文件中找到单词,与用户输入的字母相同(在本例中,当我输入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;
} 谢谢你的帮助。
发布于 2014-01-21 16:56:36
例如使用strpbrk
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char *words[] = {
"BDF", "BGFK", "JKLI", "NGJKL", "POIUE", NULL
};
char enter[8] = "ADFG";
char **p;
for(p = words; *p ; ++p){
char *word = *p;
if(strpbrk(word, enter)!=NULL){
printf("%s\n", word);
}
}
/*
BDF
BGFK
NGJKL
*/
return 0;
}发布于 2014-01-21 17:12:15
正如其他人所评论的,考虑到您的代码可能能够按原样编译和构建,您的问题在于这种方法,您需要确定完成所需的必要步骤。创建算法。以下是我认为你想做的事情的几个步骤:
问题陈述:给出了一个包含多个单词的文本文件,并且用户输入了一个或多个字符,查找文件中包含用户输入的一个或多个字符的所有单词。
步骤:(方法)
1)将文本文件中的所有单词读入字符串数组
2)将用户输入字符串输入到字符数组中
3)循环中的:检查每个字符串的每个字符的用户输入的每个字符
len = strlen(userInputStr);
for(i=0;i<numWords;i++) //numWords is number of words in file
{
len2 = strlen(word[i]);//length of next word in file
for(j=0;j<len;j++)
{
for(k=0;k<len2;k++)
{
//check each char of user input against each char of current word
}
}
}https://stackoverflow.com/questions/21263098
复制相似问题