Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C编程,我做错了什么?斯特托克_C - Fatal编程技术网

C编程,我做错了什么?斯特托克

C编程,我做错了什么?斯特托克,c,C,在与我的主.c程序保存在同一文件中的记事本中 ape apple ball bill bull foot parrot peeble season zebras zoo 例如,假设字典中有“bull”一词。单词“bull”包含1个“b”字符、2个“l”字符和1个“u”字符。现在假设输入字母是“alblldi”。在“alblldi”中,“bull”有足够的“b”字符,因为“alblldi”至少包含1个“b”字符。类似地,“alblldi”对“bull”有足够的“l”字符,因为“a

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

ape apple
ball    bill    bull
foot
parrot  peeble
season
zebras  zoo
例如,假设字典中有“bull”一词。单词“bull”包含1个“b”字符、2个“l”字符和1个“u”字符。现在假设输入字母是“alblldi”。在“alblldi”中,“bull”有足够的“b”字符,因为“alblldi”至少包含1个“b”字符。类似地,“alblldi”对“bull”有足够的“l”字符,因为“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 );
    }
}
#包括
#包括
#包括
#定义大小100
内部主(空)
{
int=0;
字符字符串[大小];
字符名称[大小];
文件*cfPtr;
字符字[大小];
char*tokenPtr;//创建字符指针
printf(“\n给我一句话:”);
fgets(字符串、大小、标准输入);
printf(“要标记的字符串是:%s\n”,字符串);
printf(“给我一个词:”);
scanf(“%s”,单词);
tokenPtr=strtok(字符串,“”;//开始标记化句子
认沽权(“”);
//继续标记化语句,直到tokenPtr变为NULL
while(tokenPtr!=NULL){
如果(!strcmp(字,令牌ptr)){
printf(“%s:这是您要查找的单词!\n”,tokenPtr);
发现=1;
}
否则{
printf(“%s\n”,tokenPtr);
}
tokenPtr=strtok(NULL,“”;//获取下一个令牌
}//结束时
如果(!找到){
printf(“在句子中找不到单词\%s\”,单词);
}
if((cfPtr=fopen(“dictionary.txt”,“r”))==NULL){
puts(“无法打开文件”);
}
否则{
fgets(名称、尺寸、cfPtr);
而(!feof(cfPtr)){
printf(“文件中的行是:%s\n”,名称);
tokenPtr=strtok(名称“\t”);//开始标记化句子
//继续标记化语句,直到tokenPtr变为NULL
while(tokenPtr!=NULL){
printf(“\t%s\n”,tokenPtr);
tokenPtr=strtok(NULL,“\t”);//获取下一个令牌
}//结束时
fgets(名称、尺寸、cfPtr);
}
fclose(cfPtr);
}
}
我会这样做:

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

int count(char *str, int c) {
    int n = 0;

    for (size_t i = 0; i < strlen(str); i++) {
        if (str[i] == c) {
            n++;
        }
    }

    return n;
}

int check_word(int counts[256], char *str) {
    if (str == NULL) {
        return 0;
    }

    for (int i = 0; i < 256; i++) {
        if (count(str, i) < counts[i]) {
            return 0;
        }
    }   

    return 1;
}

int check_words(char *word, char **words, size_t n) {
    int counts[256] = {0};

    for (size_t i = 0; i < strlen(word); i++) {
        counts[(int)word[i]]++;
    }

    int k = 0;
    for (size_t i = 0; i < n; i++) {
        if (check_word(counts, words[i])) {
            printf(">>> %s\n", words[i]);
            k++;
        } else {
            printf("    %s\n", words[i]);
        }
    }

    return k;
}

int main(void) {
    char *words[] = {
        "bull", "bill", "ball", "bubblel", "babble", "bible",
    };

    check_words("bull", words, 6);
    return 0;
}

这意味着
bull
匹配
bull
,和
bubblel
显然是家庭作业,所以我不是为你做的,但是:你可以在纸上做得很好,可以问一个明确的问题,所以只要找出如何在代码中实现它

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

此解决方案比简单的计数和比较更有效,因为它并不总是计算我们所比较的词。
>>> bull
    bill
    ball
>>> bubblel
    babble
    bible