gpt4 book ai didi

C 中的字符串数组

转载 作者:行者123 更新时间:2023-11-30 15:35:55 26 4
gpt4 key购买 nike

对于家庭作业的一部分,我需要循环提示用户输入单词,直到他们输入 20 个单词或直到他们输入单词“完成”。目前,我已经满足了两个参数,但是当我输入“完成”一词时,它也会被扫描到数组中,这不是我想要做的。

这是我当前的功能:

int row = 0;
int column = 0;
int i = 0;

while(i < 20)
{
printf("Enter words you would like hidden in the puzzle. Type 'done' when finished:\n");
scanf("%s",a[i]);

if(strcmp(a[i],"done") == 0)
{
break;
}

if((strlen(a[i]) > row) || (strlen(a[i]) > col))
{
printf("Error. Word was too long to enter into puzzle.\n");
}

else
{
i++;
}
}

数组“a”是字符串数组。我知道 scanf("%s",a[i]); 行正在将单词“done”扫描到数组中,我只是不知道如何调整它,这样就不会'不会发生。有人可以帮我解决这部分吗?

最佳答案

试试这个:

char input[256];
while(i < 20)
{
printf("Enter words you would like hidden in the puzzle. Type 'done' when finished:\n");
scanf("%s",&input);
if(strcmp(input,"done") != 0)
{
strcpy(a[i],input);
if((strlen(a[i]) > row) || (strlen(a[i]) > col))
{
printf("Error. Word was too long to enter into puzzle.\n");
}
i++;
}
else
break;
}

关于C 中的字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22752487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com