1

So I am trying to create the program that I mentioned in the title, but there is one problem with it. Here is the code.

#include <stdio.h>
#include <stdlib.h>



int main()
{

    char input[30];
    char array[][10] = {"One", "Two", "Three"};
    printf("Enter input.\n");
    scanf("%s", input);
    if(input == array[0]){
    printf("Array 0");
    } else if(input == array[1]){
    printf("Array 1");
    } else if(input == array[2]){
    printf("Array 2");
    } else {
    printf("Incorrect.");
    }







    return 0;
}

The problem is that for example when I enter "One", it prints "Incorrect" instead of "Array 0". The same thing happens when I input "Two" or "Three". It doesn't work the way I intend it to be. I want it to print "Array 0" when I type "One", "Array 1" when I type "Two", and so on.

2 Answers2

1

Just like comment mentions you need to use strcmp in order to compare strings in C. So the correct code would be:

#include <stdio.h>
#include <stdlib.h>


int main()
{

    char input[30];
    char array[][10] = {"One", "Two", "Three"};
    printf("Enter input.\n");
    scanf("%s", input);
    if(strcmp(input, array[0]) == 0)
    {
        printf("Array 0");
    }
    else if(strcmp(input, array[1]) == 0)
    {
        printf("Array 1");
    }
    else if(strcmp(input, array[2]) == 0)
    {
        printf("Array 2");
    }
    else
    {
        printf("Incorrect.");
    }

    return 0;
}

You can also reduce code to:

#include <stdio.h>
#include <stdlib.h>
int main()
{

    char input[30];
    char array[][10] = {"One", "Two", "Three"};
    printf("Enter input.\n");
    scanf("%s", input);
    for(int x = 0; x < 3; x++)
    {
        if(strcmp(input, array[x]) == 0)
        {
            printf("Array %d", x);
            return 0;
        }
    }
    printf("Incorrect.");

    return 0;
}
0

To extend on the statement "you cannot use == to compare strings in C", I'd like to mention why it doesn't work.

The equality operator compares 2 values, preferably of the same type and same levels of indirection, however implicit casting does allow different types of variables to be compared.

In C, strings are just char arrays. And arrays decay to pointers. So essentially, when you refer to input and array[i] (where i is a valid index of array), you're actually referring to the memory address of the start of that string.

So, referring to char str[10]; using str, will simply give you the address of str[0]. You can try it yourself in a simple program-

int main()
{
    char str[] = {'a', 'b', 'c'};
    printf("%p\n", str);
}

The above code snippet will print the address of char str[3];

Now when you compare the address of input with the starting address of array[i], obviously, they will not be the same.

This is actually the same for any arrays, not just char arrays, you cannot compare them with ==.

This is why you need to use strcmp to compare 2 strings.

if (!strcmp(input, array[i]))
{
    // do stuff
}

strcmp returns 0 on a full match so you can either do an explicit == 0 or simply ! the strcmp like I've done here.

More information on strcmp here

Chase
  • 5,315
  • 2
  • 15
  • 41