在与main.c程序保存在同一文件中的记事本中,

ape apple
ball    bill    bull
foot
parrot  peeble
season
zebras  zoo


例如,假设单词“ bull”在词典中。 “ bull”一词包含1个“ b”字符​​,2个“ l”字符和1个“ u”字符。现在说输入字母为“ alblldi”。在“ alblldi”中,我们有足够的“ b”字符​​表示“ bull”,因为“ alblldi”至少包含1个“ b”字符​​。同样,“ alblldi”具有足够的“ l”字符表示“ bull”,因为“ alblldi”至少包含2个“ l”字符。但是,“ alblldi”没有至少1个“ u”字符,因此我们知道我们无法从“ alblldi”中制作“ bull”。

我该如何实现?

因此,我刚刚开始键入此代码,需要帮助,到目前为止,我得到了:

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

#define SIZE 100

int main( void )
{
    int found = 0;
    char string[SIZE];
    char name[ SIZE ];
    FILE *cfPtr;
    char word[ SIZE ];
    char *tokenPtr; // create char pointer


    printf("\nGive me a sentence: ");

    fgets( string, SIZE, stdin );
    printf("The string to be tokenized is: %s\n", string);

    printf("Give me a word: ");
    scanf("%s",word);

    tokenPtr = strtok( string, "" ); // begin tokenizing sentence

    puts("");

    // continue tokenizing sentence until tokenPtr becomes NULL
    while ( tokenPtr != NULL ) {

        if (!strcmp(word,tokenPtr)) {
            printf("%s : This is the word you are looking for!\n", tokenPtr);
            found = 1;
        }
        else {
            printf( "%s\n", tokenPtr );
        }

        tokenPtr = strtok( NULL, " " ); // get next token
    } // end while

    if (!found) {
        printf("The word \"%s\" was not found in the sentence\n",word);
    }


    if ( ( cfPtr = fopen( "dictionary.txt", "r" ) ) == NULL ) {
        puts( "File could not be opened" );
    }
    else {
        fgets( name, SIZE, cfPtr );

        while ( !feof( cfPtr ) ) {
            printf( "Line from file is: %s\n", name );


            tokenPtr = strtok( name, "\t" ); // begin tokenizing sentence
            // continue tokenizing sentence until tokenPtr becomes NULL
            while ( tokenPtr != NULL ) {
                printf( "\t%s\n", tokenPtr );
                tokenPtr = strtok( NULL, "\t" ); // get next token
            } // end while

            fgets( name, SIZE, cfPtr );
        }

        fclose( cfPtr );
    }
}

最佳答案

显然是功课,所以我不是为您做功课,而是:您可以在纸上做得足够好,可以提出一个明确的问题,因此只需弄清楚如何在代码中实现即可。


每个字母的计数器'int a = 0; int b = 0; int z = 0; ...(每个字母一个变量?必须有更好的方法...)
根据输入的单词设置计数器
检查您搜索的单词是否可以满足所有要求
这里有几个容易识别的通用例程。

08-28 05:53