arrays - 我正在字符串数组上编写交流代码。我想随时检查用户输入,看看用户之前是否已经输入过相同的单词
问题描述
这是我的代码的一部分。我希望 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;
}
}*/
解决方案
推荐阅读
- javascript - 使用 React 在本地存储中设置默认数据的正确方法是什么?
- openscript - 问题:处理异常后OpenScript没有进入下一次迭代
- javascript - 如何在“深度 2”层次结构中使用道具
- php - 如果 cond 精确计数,如何制作?
- flutter - 为什么我的小部件上的参数传递为空?
- javascript - 在 axios 中使用异步等待
- python - 为什么另一个模型的上下文在模板中不起作用
- wagtail - 具有“全角”格式的图像未在 Wagtail 的 RichTextField 中居中?
- r - 学习 R:在 col-A 中查找值,在 Col-B 中列出唯一值,然后在 Col C 中列出相关值
- javascript - 找不到模块'../drop-file.js'量角器,角度