C程序对输入文件中的单词总数进行计数 [英] C program to count total words in an input file
问题描述
输入文件在第2行包含一个完全为空的行,并在文本的最后一个句号之后包含一个不必要的空格。有了这个输入文件,我得到了48个单词,而我想得到了46个单词。
Input file contains a completely empty line at line 2 and an unnecessary white space after the final full stop of the text. With this input file I am getting 48 words while I was suppose to get 46 words.
我的输入文件包含:
从一个故事开始查尔斯·达尔文(Charles Darwin)的《两座城市》
My input file contains:
"Opening from A Tale of Two Cities by Charles Darwin
那是最美好的时光,那是最糟糕的时光。这是
信仰的时代,这是令人怀疑的时代。
It was the best of times, it was the worst of times. It was the age of wisdom, it was the age of foolishness. It was the epoch of belief, it was the epoch of incredulity. "
这是我的尝试方式:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define max_story_words 1000
#define max_word_length 80
int main (int argc, char **argv)
{
char story[max_story_words][max_word_length] = {{0}};
char line[max_story_words] = {0};
char *p;
char ch = 0;
char *punct="\n ,!.:;?-";
int num_words = 1;
int i = 0;
FILE *file_story = fopen ("TwoCitiesStory.txt", "r");
if (file_story==NULL) {
printf("Unable to open story file '%s'\n","TwoCitiesStory.txt");
return (EXIT_FAILURE);
}
/* count words */
while ((ch = fgetc (file_story)) != EOF) {
if (ch == ' ' || ch == '\n')
num_words++;
}
rewind (file_story);
i = 0;
/* read each line in file */
while (fgets (line, max_word_length, file_story) != NULL)
{
/* tokenize line into words removing punctuation chars in punct */
for (p = strtok (line, punct); p != NULL; p = strtok (NULL, punct))
{
/* convert each char in p to lower-case with tolower */
char *c = p;
for (; *c; c++)
*c = tolower (*c);
/* copy token (word) to story[i] */
strncpy ((char *)story[i], p, strlen (p));
i++;
}
}
/* output array */
for(i = 0; i < num_words; i++)
printf ("story[%d]: %s\n", i, story[i]);
printf("\ntotal words: %d\n\n",num_words);
return (EXIT_SUCCESS);
}
推荐答案
您的 num_words
考虑了两个额外的空格,这就是为什么要得到48个空格的原因。
Your num_words
takes account of the two extra whitespaces, that's why you get 48.
您应该只打印 i
fgets
- strtok
循环之后,如果我没记错的话。
You should simply print i
immediately after the fgets
-strtok
loop, if I'm not mistaken.
这篇关于C程序对输入文件中的单词总数进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!