如何实现一个显示文本文件中五个最长单词的C程序?

时间:2018-03-05 08:47:03

标签: c visual-studio

文本文件有225000个单词。按以下顺序:

d
d
da
dog
dover
Denmark
lovethislifetoenjoy
beautifully
travelllerwise
lovethislifetoenjoy
alaskabluewheals
oceanisbluee
lovethislifetoenjoyfuntravel
basketball
lovethislifetoenjoyfuntravel
fashion
londonis

...

所以该程序应该打印以下内容:

lovethislifetoenjoyfuntravel

travelllerwise

alaskabluewheals

oceanisbluee

lovethislifetoenjoyfuntravel

到目前为止,我编码了这个

int main()
{
    FILE *fp;
    errno_t err;
    char current_Word[1024];
    char longest_word[1024];
    int ct=0;
    char longWordList = malloc(sizeof(char*) * 10);

        if ((err = fopen_s(&fp, "dictionary.txt", "r")) != 0) 
    {
        printf("Cannot open file.\n");
        return -1;
    }
    memset(longest_word, '\0', sizeof(longest_word));
    while(fscanf_s(fp, "%s", current_Word, _countof(current_Word))!= EOF)
    {
           if (ct == 0)
           {
           strcpy(longest_word, current_Word);
       }

       if (strlen(current_Word) > strlen(longest_word))
       {
        strcpy(longest_word, current_Word);
       }
       ct++;

    }
    /* print */
    printf("\nThe longest word is\n");
    fprintf(stdout, "%s\n", longest_word);
    fclose(fp);
    return 0;
}

该程序仅打印此 - > lovethislifetoenjoyfuntravel

如何跟踪存储空间中所有最长的单词,然后将存储区中的单词与刚从输入文件中读取的单词进行比较?

如果多个单词与第5个最长单词绑定,则结束程序并打印最终存储。

当我发现很少与存储的第5个字相关时,如何结束程序?

我用Java和Python编写代码,但这是我需要为部门计划完成的。

我正在为此项目使用Visual Studio 2017社区版。

非常感谢您的协助。

由于

2 个答案:

答案 0 :(得分:0)

没有真正需要malloc,这里定义很糟糕。

你应该为最长的单词保留一个多维数组:

char longest_word[5][1024];

还添加一个变量shortest_long_word_len:

int shortest_long_word_len = 0;
int shortest_long_word_location = 0;

然后在测试current_Word时,检查它的len与shortest_long_word_len。如果更长,则在shortest_word_word_location

中将新单词strcpy到数组
strcpy(longest_word[shortest_long_word_location], current_Word);

之后只需重新计算shortest_long_word_len和shortest_long_word_location

答案 1 :(得分:0)

以下应显示五个最长的单词 此外,它还会打印其他单词,其大小与文件中五个最长单词的最短单词相同:

#define WORD_MAX_SIZE           (50)
#define LONGEST_WORDS_COUNT     (5)

int main()
{
    char    sCurWord[WORD_MAX_SIZE+1];
    char    arrLongestWords[LONGEST_WORDS_COUNT][WORD_MAX_SIZE+1];
    int     iShortestWordIndex = 0;
    FILE*   fp;

    memset( arrLongestWords, 0, sizeof(arrLongestWords));

    if( 0 != fopen_s(&fp, "C:\\Temp\\BlaBla.txt", "r"))
    {
        printf("Cannot open file.\n");
        return -1;
    }

    while( fscanf_s( fp, "%1023s ", sCurWord, WORD_MAX_SIZE) == 1)
    {
        if( strlen(arrLongestWords[iShortestWordIndex]) < strlen(sCurWord))
            strcpy_s( arrLongestWords[iShortestWordIndex], WORD_MAX_SIZE, sCurWord);

        //  Re-calculate shortest word index:
        iShortestWordIndex    = 0;
        for( int i=1; i<LONGEST_WORDS_COUNT; i++)
        {
            if( strlen( arrLongestWords[i]) < strlen( arrLongestWords[iShortestWordIndex]))
            {
                iShortestWordIndex = i;
            }
        }
    }

    /* print */
    printf("\n%d longest words are:\n", LONGEST_WORDS_COUNT);
    for( int i=1; i<LONGEST_WORDS_COUNT; i++)
    {
        ::printf( "\t%s\r\n", arrLongestWords[i]);
    }

    ::printf( "\r\nOther words in the file, which their length are the same as the %dth largest word:\r\n", LONGEST_WORDS_COUNT);
    //  Move file-pointer back, to the beginning of the file:
    rewind(fp);
    //  Now go over file once more to find all words, which their sizes are the
    //  same as the shortest word:
    while( fscanf_s( fp, "%1023s ", sCurWord, WORD_MAX_SIZE) == 1)
    {
        if( strlen(arrLongestWords[iShortestWordIndex]) == strlen(sCurWord))
            ::printf( "\t%s\r\n", sCurWord);
    }

    fclose(fp);
    return 0;
}