有人可以帮助我使用这个比较数组吗?

时间:2016-12-14 14:43:45

标签: c new-operator

我是C的新手,我在写这个代码时遇到了麻烦。由于我的比较阵列部分,我几乎100%肯定它,但我真的不知道该改变什么。有人可以帮忙吗?如果您需要我的整个代码,我也可以发布。 代码应该将用户输入的字母与.txt文档中的单词进行比较,看看是否可以用这些字母拼写任何单词。

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 99
#define NUM_WORDS 100

void find_frequency(char string[], int count[]);
int compare_arrays(int dictionary[], int user[]);

int main()
{
    int total_words=11; //number of words
    char dictionary_words[NUM_WORDS][SIZE]; //store words in directory
    FILE *cfPtr; //dictionary.txt pointer

    if ((cfPtr=fopen("dictionary.txt","r"))==NULL)//try to open file
    {
        puts("File dictionary.txt could not be opened.");
        exit(1);//exit if file doesn't open
    }

    else{ //Read each word from the dictionary and save to array
        char line[SIZE]; //save each word

        {
            while(fgets(line,SIZE,cfPtr)!= NULL)
                  {
                      char*tokenPtr=strtok(line, "\t");
                      while(tokenPtr != NULL)
                        {
                            strcpy(dictionary_words[total_words],tokenPtr);
                            total_words++;
                            tokenPtr = strtok(NULL, "\t" );
                        }

                  }
        }
        }
    fclose(cfPtr);//close file

    char string[11];//get string of characters from user
    int count[26]={0};//store the number of each letter

    printf("Enter letters:\n");
    scanf("%s", string);


    find_frequency(string, count);//count of each character entered

    char temp[SIZE];
    int temp_count[26]={0};//convert words into letters
    int i;

    for(i=0; i<=total_words; i++);
    {
        strcpy(temp,dictionary_words[i]);
        find_frequency(temp,temp_count);//convert word to letters in alphabet

        if (compare_arrays(temp_count,count))//compare words with letters entered
        {
            printf("%s:", temp);//print what you can spell
        }
        else
        {
             printf("broken", temp);
        }
       memset(temp_count,0,sizeof(temp_count));//test next word
    }
    return(0);
}//end main

//define function
void find_frequency(char string[],int count[])
{
    int i;
    for(i=0; string[i] != '\0'; i++)
    {
        if (string[i] >= 'a' && string[i] <= 'z')
        {
            count[string[i]-'a']++;
        }
    }
}

int compare_arrays(int dictionary[], int user[])
{
    int j = 0;

    while (user[j] >= dictionary[j])
    {
        j++;
        if (j == 26)
        {
            return 0;
        }
        else
        {
            printf("also broken");
        }
    }
    return 1;
}

3 个答案:

答案 0 :(得分:1)

你输错了结果。

int compare_arrays(int dictionary[], int user[])
{
    int j = 0;

    while (user[j] >= dictionary[j])
    {
        j++;
        if (j == 26)
        {
            // You have checked all 26 letters and for all of them condition is true. Therefore a word can be made from user entered letters.
            return 1; 
        }
    }
    return 0; //Word can not be made from user entered letters
}

如果您想要处理区分大小写,

void find_frequency(char string[],int count[])
{
    int i;
    for(i=0; string[i] != '\0'; i++)
    {
        //If letter is in upper case, it will be converted to lower case before checking.
        if (tolower(string[i]) >= 'a' && tolower(string[i]) <= 'z')
        {
            count[tolower(string[i])-'a']++;
        }
    }
}

更新1:

标记化时出错。 1)int total_words=11; //number of words     您正在将此变量用作数组索引。所以它应该初始化为零。或者您为索引声明另一个变量。    int index=0;

2)strtok将返回令牌开始的地址。所以你用令牌写单词而不是复制空终止符。

    char *prevTokenPtr = line;
    while(fgets(line,SIZE,cfPtr)!= NULL)
    {
        char*tokenPtr=strtok(line, "\t");
        while(tokenPtr != NULL)
        {
            /* Copy from last token to this token. */
            int lengthToCopy = (tokenPtr  - prevTokenPtr)/sizeof(char);
            strncpy(dictionary_words[index], prevTokenPtr, lengthToCopy);
            dictionary_words[index][lengthToCopy] = '\0';

            printf("dictionary_words[%d] is [%s]\n", index, dictionary_words[index]);
            index++;

            prevTokenPtr = tokenPtr + 1; //Neglect '\t'
            tokenPtr     = strtok(NULL, "\t" );
        }

        /* Copy the last word. */
        if(NULL != prevTokenPtr)
        {
            strcpy(dictionary_words[index], prevTokenPtr);
            printf("dictionary_words[%d] is [%s]\n", index, dictionary_words[index]);
            index++;
        }
  }

请注意:

1)我假设输入是这样的。 &#34; WORD1&#34; \吨&#34; WORD2&#34; \吨&#34; WORD3&#34; \吨... \吨&#34; wordN&#34;

2)我没有测试过这段代码。打印应该可以帮助您进一步调试。

答案 1 :(得分:0)

不确定这是正确的答案,因为很难猜到你实际上想要做什么,但你可能想要这个或类似的东西:

int compare_arrays(int dictionary[], int user[])
{
    int j = 0;

    while (user[j] >= dictionary[j])
    {
        j++;
        if (j == 26)
        {
            return 0;
        }
    }
    return 1;
}

答案 2 :(得分:0)

  

我尝试打印出dictionary_words [i],但我也只是空白行。为什么会这样做?

一个小而重大的错误:

    for(i=0; i<=total_words; i++);
    {
        …

;行末尾的for导致应该是循环主体的块只能执行一次,而错误的i

相关问题