C-有人可以帮助我进行此比较阵列



im是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] != ''; 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;
}

您返回错误的结果。

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] != ''; 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将返回令牌开始的地址。因此,您正在用令牌写单词,而不是复制null终结者。

    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] = '';
            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)我认为输入就是这样。" word1" t" word2" t" word3" t ... t" wordn"

2)我尚未测试此代码。印刷品应帮助您进一步调试。

不确定这是正确的答案

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

我尝试打印dictionary_words [i],我也有一个空白行。为什么要这样做?

一个小但重大的错误:

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

for线末端的;导致该块仅执行一次循环主体,使用错误的i

最新更新