【问题标题】:C - Find number of times each word appears in an inputC - 查找每个单词在输入中出现的次数
【发布时间】:2016-05-03 19:53:39
【问题描述】:

我的任务是创建一个函数

打印一个表格,以相同的顺序显示每个不同单词在文本中出现的次数

我正在使用gets() 读取字符串,这是一个将字符串中的每个单词标记化并将其存储在二维数组中的函数,并且需要帮助弄清楚如何制作一个分析数组以查找重复项的函数.

这是分词器功能:

void tokeniz(char *array)
{
    char words[arraySize][arraySize] = { NULL };

    const char s[2] = " ";
    char *token;
    int i = 0;

    token = strtok(array, s);

    while (token != NULL)
      {
        strcpy(words[i], token);
        token = strtok(NULL, s);
        i++;
      }
    wotable(words);
}

在程序的早期,我有一个函数来计算每个字符在字符串中出现的次数(预标记)。我可以重新利用其中的一些代码吗?

    void   alpha(char *array)
{
    char character = 'a';
    int numberOf = 0, tcc = 0;

    for (character = 'a'; character <= 'z'; character++)
    {
        tcc = (tcc + numberOf);
        numberOf = 0;
        for (int i = 0; i < arraySize; i++)
            if (*(array + i) == character)
                numberOf++;
        if (numberOf != 0)
            printf("\nNumber of %c's:\t\t%d\n", character, numberOf);
    }
    printf("\nTotal character count:\t%d\n\n- - - - - - - - - - - - - -", tcc);
}

【问题讨论】:

  • 不显示代码很难说——但你不太可能没有
  • 您应该很少使用gets(),阅读link here 以了解原因。请改用fgets()
  • 不知何故,我觉得你的 alpha 例程倒退了。想象一下,你想为单词写同样的东西;您会遍历本地字典文件并测试其中的每个单词以查看它是否在您的输入字符串中吗?
  • 1) 使用gets() 2) 在需要0'\0' 时使用NULL 进行初始化 3) strcpy() 没有长度保护都是最好避免的弱实践。
  • 函数gets() 已经贬值了几年,并完全从C11 标准中删除。你的编译器应该已经告诉你了。强烈建议将gets()替换为fgets()(一定要阅读man page,因为参数完全不同。

标签: c arrays


【解决方案1】:

不,您将无法重新利用其中的某些代码。

【讨论】:

    【解决方案2】:

    以下代码:

    1. 干净编译
    2. 需要在几个地方添加几行代码
    3. 有点浪费,因为每个新词都会调用realloc()
    4. 正确检查错误
    5. 可能希望将调用fgets() 替换为调用readline()(请务必阅读/理解readline() 的手册页)
    6. 可以将内存的释放作为清理功能的一部分分开,这样代码只需要编写一次。

    现在是代码

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_BUF_LEN (1024)
    
    struct wordsAndCount
    {
        char *pWord;
        size_t  count;
    };
    
    
    // note: following assumes words are not continued across multiple lines
    int main( int argc, char *argv[] )
    {
        if( 2 != argc)
        {
            fprintf( stderr, "USAGE: %s <inputFileName>\n", argv[0]);
            exit( EXIT_FAILURE );
        }
    
        // implied else, correct number of command line arguments
    
        FILE *fp = NULL;
        if( NULL != (fp = fopen( argv[1], "r") ) )
        { // fopen failed
            perror( "fopen for input file failed" );
            exit( EXIT_FAILURE );
        }
    
        // implied else, fopen successful
    
        struct wordsAndCount **table = NULL;
        size_t   countWords = 0;
    
        char buffer[ MAX_BUF_LEN ];
        char *token = NULL;
        char *delimeters = ",.;: '\"\n";
    
        while( fgets( buffer, sizeof buffer, fp ) )
        {
            token = strtok( buffer, delimeters );
            while( NULL != token )
            {
                struct wordsAndCount ** temp = realloc( table, (countWords+1)*sizeof (struct wordsAndCount *) );
                if( !temp )
                { // then realloc failed
                    perror( "realloc failed" );
                    fclose( fp );
    
                    for( ; countWords; countWords-- )
                    {
                        free( (*table[countWords]).pWord );
                    }
                    free( table );
                    exit( EXIT_FAILURE );
                }
    
                // implied else, realloc successful
    
                table = temp;
    
                int foundIndex = 0;
                // if word already in table[] <-- need to add code for this
                    (*table[foundIndex]).count++;
                //else
                {
                    (*table[countWords]).pWord = strdup( token );
                    if( !(*table[countWords]).pWord )
                    { // then strdup failed
                        perror( "strdup failed" );
                        fclose( fp );
    
                        for( ; countWords; countWords-- )
                        {
                            free( (*table[countWords]).pWord );
                        }
                        free( table );
                        exit( EXIT_FAILURE );
                    }
    
                    // implied else, strdup successful
    
                    (*table[countWords]).count = 1;
                    countWords++;
                }
    
                token = strtok( NULL, delimeters );
            } // end while tokens
        } // end while more lines in input file
    
        // print words and counts <-- need to add code for this
    } // end function: main
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      相关资源
      最近更新 更多