首页 > 解决方案 > 我正在字符串数组上编写交流代码。我想随时检查用户输入,看看用户之前是否已经输入过相同的单词

问题描述

这是我的代码的一部分。我希望 screenWord 函数接收整个数组(用户输入值)和当前输入字。它应该检查当前单词是否已经在数组中。如果它被输入,那么函数应该打印出“这个词已经存在”。

int screenWord (char (*arrOfWords)[20],char* word,int count)
{
    //this will break out of the loop calling the function
    if (strcmp(word,"end")==0)
    {
        return 0;
    }
    
    //the part i use to check
    for (int i=0;i<count+1;i++)
    {

        /*i print out to see what value is currently there
        printf("%s %s\n",arrOfWords[i], word);*/

         // i want it to continue checking the whole array if the current word is not equal to the ith word, but it does not work.
        if (!(strcmp(arrOfWords[i],word)))
        {
            continue;
        }
        else 
        {
          printf("This word already exists. Please enter another word");
        }      
    }
}

我应该让代码看起来像这样。

输入一个单词(输入'end'退出):tiger

输入一个单词(输入'end'退出):cat

输入一个单词(输入'end'退出):cat

这个词已经存在。请输入另一个字。

输入一个单词(输入'end'退出):eagle

输入一个单词(输入'end'退出):end

列表中的 5 个单词:tiger dog cat eagle mouse

输入要搜索的单词(输入'end'退出):tiger

这个词在列表中。

输入要搜索的单词(输入'end'退出):bird

这个词不在列表中。

输入要搜索的单词(输入'end'退出):end

/*this is my full code
#include <stdio.h>
#include <string.h>

char* askWord (char *str);
void printArray (char (*arrOfWords)[20],int count);
void searchWord (char (*arrOfWords)[20],int count);
int main() {
    
    char arrOfWords [10][20];
    int count=0;
    for (int i=0;i<10;i++)
    {
        char* checkWord= askWord(arrOfWords[i]);
        count++;
        int result= screenWord (arrOfWords, checkWord,count);
        
        if (result==0)
        {
            break;
        }
        else if (result==1)
        {
            printf("This word already exists. Please enter another word.\n");
        }
        
    }
    
    printArray(arrOfWords,count);

    searchWord(arrOfWords,count);
    
    return 0;
    
}
char* askWord (char *str)
{
    printf("\nEnter a word (Enter 'end' to quit): ");
    scanf("%s",str);
    return str;
}

int screenWord (char (*arrOfWords)[20],char* word,int count)
{
    if (strcmp(word,"end")==0)
    {
        return 0;
    }
    
    
    for (int i=0;i<count;i++)
    {
        printf("%s %s\n",arrOfWords[i], word);
        if (arrOfWords[i+1] && strcmp(word, arrOfWords[i+1])==0)
        {
            return 1;
        }
        else 
        continue;
    }
}

void printArray (char (*arrOfWords)[20],int count)
{
    printf("%d words is in the list\n",count-1);
    for (int i=0;i<count-1;i++)
    {
        printf("%s ",arrOfWords[i]);
    }
}

void searchWord (char (*arrOfWords)[20],int count)
{   
    char searchWord[20];
    int i=0;
    printf("\nEnter a word to search (Enter 'end' to quit):");
    scanf("%s",searchWord);
    while(strcmp(searchWord,"end")!=0)
    {
             if (strcmp(searchWord,arrOfWords[i])==0)
            {

                printf("This word is in the list.\n");
                //continue;
            }
        else
            {
                printf("This word is NOT in the list.\n");
                //continue;
            }
        i++;
       
        printf("\nEnter a word to search (Enter 'end' to quit):");
        scanf("%s",searchWord);
        i=0;
        
    }
}*/
    

标签: arrayscstringpointers

解决方案


推荐阅读