我必须从文本文件中读取所有单词并输出单词总数、不同单词的数量和最常用的单词。我仍然是初学者,所以任何帮助都很棒。
阅读单词时,连字符/撇号/标点符号被省略,因此 O'connor 与 Oconnor 是同一个单词。<---我不知道该怎么做,所以任何帮助都会很棒。
这是我到目前为止所拥有的,但是现在当我尝试编译它时,它会给我一个警告,strcpy
并说我没有正确使用它。单词总数的输出有效,但它给我的不同单词的数量为 0,而最常用的单词则没有。
任何帮助都会很棒,谢谢!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int number=0;
int i;
char *temp;
char *temp2;
char word[2500][50];
int wordCount=0;
int mostFreq=1;
char mostFreqWord[2500][50];
int frequentCount=0;
int distinctCount=0;
int j;
char *p;
FILE *fp;
//reads file!
fp= fopen("COEN12_LAB1.txt", "r");
if(fp == NULL) // checks to see if file is empty
{
printf("File Missing!\n");
return 0;
}
while(fscanf(fp,"%s", word) == 1) //scans every word in the text file
wordCount++; //counts number of words
while(fscanf(fp,"%s",word) == 1)
{
for(i=0;i<wordCount;i++)
{
temp=word[i];
for(j=0;j<wordCount;j++)
{
temp2 = word[j];
if(strcmp(temp,temp2) == 0) //check to see if word is repeated
{
frequentCount++;
if(frequentCount>mostFreq)
{
strcpy(mostFreqWord,word[i]); //this doesn't work
}
}
distinctCount++;
}
}
}
printf("Total number of words: %d\n", wordCount);
printf("Total number of distinct words: %d\n", distinctCount);
printf("The most frequently appeared word is: %s \n", &mostFreqWord);
fclose(fp);
}