C语言 单词检索程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言 单词检索程序相关的知识,希望对你有一定的参考价值。

该程序能够在一篇英文文章中检索出指定的单词的位置,如果有多处出现该单词,则逐个显示其位置,如果一个符合要求的单词都没有,则返回检索失败通知用户
只要没错误能运行,我把所有的分都给你了,能成功运行剩下的分都追加给你
二楼的幸苦了,仔细看了一下你的,好像有点出入,不是自己输入文章,是打开已有文章。还得麻烦你稍稍修改下。谢谢哈

=====================================
问题补充:二楼的是死循环运行不了啊
=====================================
实在抱歉,之前疏忽了,现在已经改好了,再试一下吧:)

=====================================
问题补充:二楼的幸苦了,仔细看了一下你的,好像有点出入,不是自己输入文章,是打开已有文章。还得麻烦你稍稍修改下。谢谢哈
=====================================
根据你的要求,又改了一版,现在已经改好了,再试一下吧:)
给:
#include<stdio.h>
#include<string.h>
#define MAX_size 1000
int flag=1,degree=0;
void Index(char str[],char word[],int position[])

int i,len_str,len_word,pos_str,pos_word,k=0,word_number=0;//word_number代表短文中单词的个数
len_word=strlen(word);
len_str=strlen(str);
for(i=0;i<len_str;i++)

while(str[i]==' '||str[i]==','||str[i]=='.')
i++;
word_number++; //单词个数加一
for(pos_str=i,pos_word=0;pos_str<len_str && pos_word<len_word;pos_str++,pos_word++)

if(str[pos_str]!=word[pos_word])
break;

if(pos_word==len_word && (str[pos_str]=='\0'|| str[pos_str]==' '||str[pos_str]==','||str[pos_str]=='.')) //表明找到相等的单词

position[k++]=word_number;
degree++; //相等的次数加1
flag=0;

else

while(str[pos_str]!=' '&&str[pos_str]==','&&str[pos_str]=='.'&& pos_str<len_str)
pos_str++;

i=pos_str;


void main()

char str[MAX_size],word[20],ch;
int position[100],i;

int k=0;
FILE *fp;

if((fp=fopen("a.txt","r"))!=NULL)

while(1)

ch=fgetc(fp);
if(ch==EOF) break;

str[k]=ch;
k++;



printf("请输入要检索的单词: \n");
gets(word);
Index(str,word,position);
if(flag)
printf("您输入的单词不在短文中。\n");
else

printf("您输入的单词在短文中,它共出现 %-d 次\n",degree);
printf("出现的位置为: \n");
for(i=0;i<degree;i++)
printf("第%-2d个单词\n",position[i]);

fclose(fp);
参考技术A 在 E:盘下建立一个test文件夹,然后在文件夹里建一个english1.txt文档,将要搜索的文章复制进去,运行试试。
或者自己在源程序里修改路径,注意路径里有两条反斜线。
因为时间不多,没有系统的测试,你自己试试吧

ps:1.后来重写了比较函数,修复了在一个单词后紧接标点不能被检索到的问题
2.注意将记事本 格式里的自动换行功能取消
3.检索区分大小写

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

#define len 30 // 定义单词长度最大为30

#define loc "E:\\test\\english1.txt" //要搜索的文件路径,
//注意中间有 两个 反斜线!!!!!

char key[len]; //存储你要查找的关键字
char str[len]; //存储每次从你的文件中读取的词;
int num = 0; //初始化 搜索的 关键字个数为0;

int deal(FILE * fp,int cbase) // 当搜索到一个关键字时进行处理的函数可以自己根据要求修改;
// fp 为你搜索文件的指针,
//cbase 存储了该文件开始的地址
char ch; //读取文件的每个字符,
//用于查找文件中换行符的个数以确定找到的关键字位置
FILE * fpch = fopen(loc,"r") ;

ch = getc(fpch);
fpch ->_base = (char *)cbase;
fpch ->_ptr = (char *)(cbase + 1);
int cnt_line = 0; //换行符个数初始化为0;
while(fpch ->_ptr != fp ->_ptr )

ch = getc(fpch);
if(ch == 10) //换行符LF ASCII码为10;

cnt_line++;


printf("第%d行找到你要搜索的关键字\n",cnt_line + 1);
num++;
return 0;

int cmp(char * key, char * str)

char * p = key;
char * s = str;
while((*p != 0) && (*s != 0) && (*p == *s))

p++;
s++;

if(*p == 0 && *s == 0)
return 1;
else if(*p == 0)

if(*s >= 97 && *s <= 122)
return 0;
else
return 1;

else
return 0;


int search(FILE * fp) //搜索函数,fp为要搜索的文件指针


while(scanf("%s",str) != EOF)


if(1 == cmp(key, str)) //单词与关键字匹配;


int cbase = (int)fp ->_base;
deal(fp,cbase);


return 0;


int main()


printf("请输入关键字\n");
scanf("%s",key);

FILE * fp = freopen(loc,"r",stdin); //打开要搜索的文件;
if(NULL == fp)

printf("你输入的文件打开失败\n");
exit(0);


search(fp);
printf("共搜索到%d处关键字\n",num);

return 0;
参考技术B 我只有一个通过关键字来查找图书的程序,查找部分算法是相似的,你可以自己去修改下,VS2008下编译通过
#include "stdafx.h"
#include <string.h>

struct index

int bookTitel;
char bookName[20];
struct index *next;
;

int myStrcmp(char *charString,char keyString[10]) //判断函数

int judge = 1;
int counter = 0;
char *pKey = NULL;
char *pRestoration = NULL;
pKey = keyString;
pRestoration = charString;

while(*charString != '\0' && *pKey != '\0')

if(*charString == *pKey)

while(*charString == *pKey)

charString ++;
counter ++;
pKey ++;
if(*pKey == '\0')

judge = 0;
return judge;



else

charString ++;
counter ++;
pKey ++;

charString = pRestoration;
charString = charString + counter;
pKey = keyString;

return judge;


void searchFunction(char key[10],struct index *searchBook) //查找函数

int NothingOutput = 0;
printf("titel\tname\n\n");
while(searchBook != NULL)

if(myStrcmp(searchBook -> bookName,key) == 0)

printf("%d\t%s\n",searchBook -> bookTitel,searchBook -> bookName);
NothingOutput = 1;

searchBook = searchBook -> next;

if(NothingOutput == 0)

printf("没有记录!\n");



int main()

struct index *linkHead = NULL;
struct index *pTail = NULL;
struct index *pOutput = NULL;
int autoTitel = 1;
char scanName[20];
char searchKey[10];

printf("没有信息!请输入图书名称:\n");
struct index *newBook = (struct index *)malloc(sizeof( struct index));
scanf("%s",scanName);
while(strcmp("end",scanName) != 0)

struct index *newBook = new struct index;
newBook -> next = NULL;
strcpy(newBook -> bookName,scanName);
newBook -> bookTitel = autoTitel;
autoTitel++;
if(linkHead == NULL)

linkHead = newBook;
pTail = newBook;

else

pTail -> next = newBook;
pTail = pTail -> next;

scanf("%s",scanName);

pOutput = linkHead;
printf("titel\tname\n\n");
while( pOutput != NULL)

printf("%d\t%s\n",pOutput -> bookTitel,pOutput -> bookName);
pOutput = pOutput -> next;


printf("请输入一个关键字:\n");
scanf("%s",searchKey); // !!!
searchFunction(searchKey,linkHead);
return 0;
参考技术C 前几天我也碰见个这个题目,你看看我的这个怎么样
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mbstring.h>

void main()

FILE *stream;
char filename[40];///保存文件路径
char temp[30];///保存临时的单词
int ch,count=0,i=0;
char string[30];///保存输入的要比较的单词

printf("请输入文件路径!\n");
gets(filename);
printf("请输入要检索的单词\n");
gets(string);
if((stream=fopen(filename, "rb"))==NULL)

perror("open the file error!\n");
exit(1);
/////打开文件
while((ch=fgetc(stream))!=EOF)///逐个取词至文件尾

if(_ismbcalpha(ch))


temp[i++]=(char)ch;///将单词临时保存到temp里面
temp[i]='\0';
if(strcmp(temp,string)==0)

++count;
printf("这是第%d个相等的单词。\n,位置是:%ld\n", count,ftell(stream));

/////比较单词和输入的单词是不是相等。

else


i=0;



if(count==0)
printf("检索的单词没找到!\n");
参考技术D 你用什么编译环境。发邮件给我。我发一个给你luopro1106@163.com

C语言试题八十之统计单词个数

1、题目

终端输入一行字符串,单词和单词之间都是以空格连着,求单词的一共个数。

2 、温馨提示

      C语言试题汇总里可用于计算机二级C语言笔试、机试、研究生复试中C程序设计科目、帮助C语言学者打好程序基础、C语言基础,锻炼您的逻辑思维和解决问题的能力,帮助你成为C语言笔试、机试解题高手,帮助你拿到C语言工作的offer,每一个题目都附带源代码和运行结果,让你不再有任何困惑,妈妈再也不担心我的C语言了。如果程序有任何看不懂或者哪个步骤不理解的地方都可以加我微信(15874274916)私聊我,或者有更好的思路解决办法都可以加我微私聊,一起探讨、希望可以正真的帮助到你,同时也可以扫下面的二维码关注我微信公众号,比如你还对生活、美食或者其它方面都感兴趣,都可以加微信交流,博主擅长讲解计算机相关方面的知识、如果大家对计算机方向、前端、移动端、服务端、数据结构、算法、网络、高数等等一些列方向比较迷茫或者不知道要学哪些?或者具体怎么学?或者其它方向有任何困惑都可以找我交流,谢谢大家关注。

3、源代码实现

#include <stdio.h>
#include <windows.h>

void fun()
{
    printf("输入一行字符:\\n");
    char ch;
    int i,count=0,word=0;
    while((ch=getchar())!=\'

以上是关于C语言 单词检索程序的主要内容,如果未能解决你的问题,请参考以下文章

c语言:编写一个程序找出一组单词中的“最小“和“最大“的单词(单词在字典中的先后顺序,字典中先出现

C语言,输入一行文字,单词间以空格分隔,然后分离其中的单词按每行一个单词输出,程序有了,求解释

C语言程序设计C语言统计单词个数,单词个数算法

输出最短单词 C语言

C语言试题八十之统计单词个数

C语言试题八十之统计单词个数