【问题标题】:Read input text into an array of words and get rid of punctuation marks将输入文本读入单词数组并去掉标点符号
【发布时间】:2015-04-17 12:06:33
【问题描述】:

我在互联网上找到了这个巨大的代码。它是一个程序,可以在文件中找到 n 个最常用的单词并将它们打印出来。以下程序读取给定的文本文件,但我想自己编写输入文本,所以我可能会将单词存储在数组中。我该怎么做才能使程序读取随机长度的文本并且以下程序仍然可以工作?而且,如果输入文本中有标点符号,我将不得不去掉它们,这样文本就不会只包含从“a”到“z”的字母。那我还需要MAX_CHARS 常量吗?

#include <stdio.h>
#include <string.h>
#include <ctype.h>

# define MAX_CHARS 26
# define MAX_WORD_SIZE 32000


// A utility function to show results, The min heap
// contains n most frequent words so far, at any time
void displayMinHeap( MinHeap* minHeap )
{
    int i;

    // print top N word with frequency
    for( i = 0; i < minHeap->count; ++i )
    {
        printf( "%s %d\n", minHeap->array[i].word,
                            minHeap->array[i].frequency );
    }
}

// The main funtion that takes a file as input, add words to heap
// and Trie, finally shows result from heap
void printKMostFreq( FILE* fp, int n )
{
    // Create a Min Heap of Size n
    MinHeap* minHeap = createMinHeap( n );

    // Create an empty Trie
    TrieNode* root = NULL;

    // A buffer to store one word at a time
    char buffer[MAX_WORD_SIZE];

    // Read words one by one from file.  Insert the word in Trie and Min Heap
    while( fscanf( fp, "%s", buffer ) != EOF )
        insertTrieAndHeap(buffer, &root, minHeap);

    // The Min Heap will have the n most frequent words, so print Min Heap nodes
    displayMinHeap( minHeap );
}

int main()
{
    int n;
    scanf("%d", &n);
    FILE *fp = fopen ("file.txt", "r");
    if (fp == NULL)
        printf ("File doesn't exist ");
    else
        printKMostFreq (fp, n);
    return 0;
}

【问题讨论】:

  • TL;博士!请将代码缩小到您遇到问题的部分,或者更好的是,请创建一个Minimal, Complete, and Verifiable Example 并向我们展示。
  • 您显示的代码缺少某些部分,例如MinHeap 的定义。

标签: c arrays input punctuation


【解决方案1】:

可以修改这个程序来做你想做的事,但我不会为你做那件事,至少不会没有报酬。对于一个简单的解决方案,请尝试生成您的文本并将其写入文本文件。然后,您可以将该文件的内容传递到字数统计软件中。你也可以使用管道来做到这一点。

【讨论】:

  • @lol 你在用什么操作系统?谷歌可以告诉你什么是管道。如果不查阅文档和大量阅读,您将无法在编程中走得更远。
猜你喜欢
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 2011-08-18
相关资源
最近更新 更多