计算文件中单词的出现次数 [英] Counting occurrences of a word in a file
本文介绍了计算文件中单词的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道这是一个正确的地方,但无论如何你要去:
我正在尝试计算所有出现的单词都在文件中但是我不知道怎么做但我只能在用户输入而不是从文件中输入;
EX:
他们有文件
50
是20
on 5
等等。但我无法让它工作。
我希望有人可以帮助我。
I dont know if this a right place to ask but anyway here you go:
I am trying count all occurrences word are that are in the file but i dont know how to do it but I can only do in like user have input in not from a file;
EX:
in file they have
the 50
is 20
on 5
and so on. but i couldnt get it to work.
I hope someone could help me out.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i = 0, j = 0, count = 0;
char str1[100], str2[20], str3[20];
//clrscr();
printf("Enter the text: ");
gets(str1);
printf("Enter word to count: ");
gets(str2);
while (str1[i] != '\0')
{
while (str1[i] != ' '&&str1[i] != '\0') //copying the word from the text to a new string
str3[j++] = str1[i++];
str3[j] = '\0'; //assigning null character at the end of string
j = 0;
if ((_strcmpi(str2, str3)) == 0) //comparing the given word with the copied word
count++;
if (str1[i] == '\0')
break;
else
i++;
}
printf("No. of words are %d", count);
_getch();
}
推荐答案
这不难:在一个循环中从文件中读取一个单词(你可以使用< a href =http://www.cplusplus.com/reference/cstdio/fscanf/> fscanf [ ^ ]用于此目的)并检查它是否与用户输入匹配。
It is not difficult: in a loop read from the file a word at time (you may use fscanf[^] for the purpose ) and check if it matches the user input.
这篇关于计算文件中单词的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文