Pratya Duti Mishra
Pratya Duti Mishra

Reputation: 11

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

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("Which one do you want to select : \n");
    
    for(int i=0 ; i<9 ; i++){
        printf("%s \n",List[i]);
        
    }
    
    printf("Please select One : ");
    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("Sorry your selected itemm is not valid. Please enter a valid selection \n");
        converter();
    }
    
    printf("Ok we can proceed now");
    
}

After running the code I get the following output

output of the above code

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("Which one do you want to select : \n");
    
    for(int i=0 ; i<9 ; i++){
        printf("%s \n",List[i]);
        
    }
    
    printf("Please select One : ");
    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("Sorry your selected itemm is not valid. Please enter a valid selection \n");
        converter();
    }
    
    printf("Ok we can proceed now");
    
}

The output :- output after commenting

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.

Upvotes: 1

Views: 97

Answers (1)

John Zwinck
John Zwinck

Reputation: 249464

This code is undefined behavior:

char convert[] = "";
scanf("%s",&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:

Upvotes: 3

Related Questions