如何在C中的for循环中比较一个”字符串用户输入”和”数组中的字符串列表”。

huangapple go评论82阅读模式
英文:

How to compare a "string user input" with a "list of string in a array" inside a for loop in C

问题

I've translated your code into Chinese as requested. Here it is:

我是C编程语言的新手,正在进行一些实际操作。
我想要使用for循环将用户输入的字符串与数组中的字符串列表进行比较。我的代码看起来像这样:-

```c
#include <stdio.h>
#include <string.h>

void converter();

int main() {
    
    converter();
    return 0;
}

void converter(){
    char convert[] = "";
    char store[] = "";
    int compare;
    char List[10][4] = {"ABC","DEF","GHI","JKL","MNO","PQR","STU","VWX","YZA"};
    
    printf("您想要选择哪一个:\n");
    
    for(int i=0 ; i<9 ; i++){
        printf("%s \n",List[i]);
        
    }
    
    printf("请选择一个:");
    scanf("%s",&convert);
    
    for (int j=0 ; j<9 ; j++) {
        printf("%d \n",strcmp(convert,List[j]));
        
        if (strcmp(convert,List[j]) == 0){
            compare = 1;
            break;
            
        }
        
    }
    
    if (compare != 1){
        printf("抱歉,您选择的项目无效。请重新输入有效选择\n");
        converter();
    }
    
    printf("好的,现在我们可以继续了");
    
}

在运行代码后,我得到了以下输出:以上代码的输出

尽管用户输入的字符串与列表中的字符串相同,但strcmp()函数并没有返回0。但我还观察到另一件事,如果我将for循环内的if条件注释掉,然后strcmp()函数将返回0,就像printf打印的那样。

在注释掉if函数后的代码:

#include <stdio.h>
#include <string.h>

void converter();

int main() {
    
    converter();
    return 0;
}

void converter(){
    char convert[] = "";
    char store[] = "";
    int compare;
    char List[10][4] = {"ABC","DEF","GHI","JKL","MNO","PQR","STU","VWX","YZA"};
    
    printf("您想要选择哪一个:\n");
    
    for(int i=0 ; i<9 ; i++){
        printf("%s \n",List[i]);
        
    }
    
    printf("请选择一个:");
    scanf("%s",&convert);
    
    for (int j=0 ; j<9 ; j++) {
        printf("%d \n",strcmp(convert,List[j]));
        
        //if (strcmp(convert,List[j]) == 0){
        //    compare = 1;
        //    break;
            
        //}
        
    }
    
    if (compare != 1){
        printf("抱歉,您选择的项目无效。请重新输入有效选择\n");
        converter();
    }
    
    printf("好的,现在我们可以继续了");
    
}

输出:注释后的输出

因此,我无法比较用户输入与数组列表。

我想从代码中实现的目标是:向用户显示一个字符串数组中的所有项目,然后用户将输入一个字符串,代码将使用for循环将输入的字符串与数组中已定义的字符串进行比较。
如果用户输入的字符串与数组中的任何字符串匹配,则我们可以继续代码的下一部分,否则将重复该函数,要求用户重新输入字符串,直到其与数组中的列表匹配。用户将始终知道自己输入的内容,因为我们首先显示列表项目,然后要求选择其中一个。

请帮我找到一种比较用户输入字符串并将其与字符串数组列表进行比较的解决方案。同时,请告诉我我哪里错了。

感谢您提前的帮助。


<details>
<summary>英文:</summary>

I am new to C programming language and doing some hands on.
I wanted to compare a string user input with a list of strings in a array using a for loop. My code looks something like this :-



#include <stdio.h>
#include <string.h>

void converter();

int main() {

converter();
return 0;

}

void converter(){
char convert[] = "";
char store[] = "";
int compare;
char List[10][4] = {"ABC","DEF","GHI","JKL","MNO","PQR","STU","VWX","YZA"};

printf(&quot;Which one do you want to select : \n&quot;);

for(int i=0 ; i&lt;9 ; i++){
    printf(&quot;%s \n&quot;,List[i]);
    
}

printf(&quot;Please select One : &quot;);
scanf(&quot;%s&quot;,&amp;convert);

for (int j=0 ; j&lt;9 ; j++) {
    printf(&quot;%d \n&quot;,strcmp(convert,List[j]));
    
    if (strcmp(convert,List[j]) == 0){
        compare = 1;
        break;
        
    }
    
}

if (compare != 1){
    printf(&quot;Sorry your selected itemm is not valid. Please enter a valid selection \n&quot;);
    converter();
}

printf(&quot;Ok we can proceed now&quot;);

}



After running the code I get the following output

[output of the above code](https://i.stack.imgur.com/gixeZ.png)

The strcmp() is not giving the output as 0 even if the string the user entered and string in the list is same. 
But yet another thing I observe, if I just comment out the if condition inside the for loop then the strcmp() is giving output 0 as printed by printf . 

Code after commenting the if function :-


#include <stdio.h>
#include <string.h>

void converter();

int main() {

converter();
return 0;

}

void converter(){
char convert[] = "";
char store[] = "";
int compare;
char List[10][4] = {"ABC","DEF","GHI","JKL","MNO","PQR","STU","VWX","YZA"};

printf(&quot;Which one do you want to select : \n&quot;);

for(int i=0 ; i&lt;9 ; i++){
    printf(&quot;%s \n&quot;,List[i]);
    
}

printf(&quot;Please select One : &quot;);
scanf(&quot;%s&quot;,&amp;convert);

for (int j=0 ; j&lt;9 ; j++) {
    printf(&quot;%d \n&quot;,strcmp(convert,List[j]));
    
    //if (strcmp(convert,List[j]) == 0){
    //    compare = 1;
    //    break;
        
    //}
    
}

if (compare != 1){
    printf(&quot;Sorry your selected itemm is not valid. Please enter a valid selection \n&quot;);
    converter();
}

printf(&quot;Ok we can proceed now&quot;);

}



The output :-
[output after commenting](https://i.stack.imgur.com/1H1Vy.png)

So, I am not able to compare the user input from the array list.
 

What I want to achieve from the code :-

The user will shown all the items in an string array using a for loop. Then the user will input a string, from the shown list, then the code will compare the input string with the already defined strings in that array by using a for loop. 
If the user entered string is matching with any of the string in the array then, we can proceed to the next section of the code, otherwise will repeat the function to ask again the user to enter a string input until and unless it matches with the list in the array. The user will always know what he/she is entering as we are first showing the list items and then asking to choose once from them.


Please help me out with a solution to compare the user input as a string and compare it with a list of string array. 

Also let me know where am I doing wrong.

Thanks in advance for the help.

</details>


# 答案1
**得分**: 3

这段代码是未定义行为:

```c
char convert[] = "";
scanf("%s", &convert);

convert 的大小为1个字符(空字符终止符)。所以它绝对没有足够的空间来存储任何文本。

你需要为任何字符串传递给 scanf() 提供最大缓冲区大小,并且需要检查 scanf() 的返回值。

如果你使用以下工具构建程序,就可以自动检测到这种错误以及类似的错误:

英文:

This code is undefined behavior:

char convert[] = &quot;&quot;;
scanf(&quot;%s&quot;,&amp;convert);

The size of convert is 1 character (the null terminator). So there is absolutely no space to store any text in it.

You need to pass the maximum buffer size to scanf() for any strings, and you need to check the return value of scanf().

This error and others like it would be automatically detected if you built your program with these tools:

huangapple
  • 本文由 发表于 2023年6月25日 19:24:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定