Emir Isakovic
Emir Isakovic

Reputation: 39

Searching for an element in 2D array, C programming

I'm a noob at C programming and I'm having some difficulties making a string list and searching for a specific element.

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

# define MAX 6
int main(){
    char word[MAX];
    char x[MAX][20];
    int i;

    strcpy(x[0], "One");
    strcpy(x[1], "Two");
    strcpy(x[2], "Three");
    strcpy(x[3], "Four");
    strcpy(x[4], "Five");
    strcpy(x[5], "Six");
    printf("%s", "Search:");
    scanf("%s", word);

    for (i=0; i<6; i++) {
        if (x[i] == word) {
            printf("%s", "Found a match!");
        }
    }

    return 0;
}

It's never executing the statement present in the if block (i.e, printf("Found a match!")) . Any idea why it is not executing the above mentioned statement? Thanks!

Upvotes: 2

Views: 2220

Answers (6)

mohan saran
mohan saran

Reputation: 11

In c a predefined function is present in string.h library it is strcmp as stated by other users function int strcmp(const char *str1, const char *str2) compares the string pointed to bystr1 to the string pointed to by str2.u can write your own function for comparing strings and use it. I want you to conceptually understand why we can't use == in c unlike c++ as c don't contain anything like string class(c is purely procedural) so that u can create object of it and use it.hence c uses char array to represent a string .if u examine ur code x[i] == word compares starting addresses of char arrays/strings x[i],word. I believe u understood the concept . now I want to explain that u can use pointers here i.e

if (*x[i] == *word) 
    printf("Found a match!");                 

Works fine as u can understand that here we are comparing two strings directly by pointing to their address locations.sorry if I have provided unwanted info due to my inexperience in SO as this my first answer in SO.

Upvotes: 1

Sakib Ahammed
Sakib Ahammed

Reputation: 2480

use strcmp() function for comparing two string. when two string is match its result is 0. so you can change like :

if ( ! strcmp(word,x[i]) )  // when match result will be `! 0 = 1`
    printf("Found Match\n");

Upvotes: 0

porcupine
porcupine

Reputation: 43

in the C programming language, the == operator is not working for comparing strings(as others wrote before me). I advice to try using a really nice feature in C++ called string. It is builded in the standard library, and with this, you can use the == operator for comparing.

#include <iostream>
using namespace std;

int main(void)
{
    string a = "Apple";
    if( a == "Apple" ) cout << "This is working" << endl;
}

Upvotes: -3

haccks
haccks

Reputation: 106042

It never returns "Found a match!". Any idea why?

Reason:
In C, array names are converted to pointers to their first elements ( with some exceptions there). x[i] == word is comparing two pointers instead of comparing strings. Since the base addresses of both arrays are different, comparison returns a false value.

Correction:

Use strcmp to compare two strings.

Upvotes: 6

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

This

if (x[i] == word)

should be

if (strcmp(x[i], word) == 0)

Upvotes: 4

Gopi
Gopi

Reputation: 19864

Use

if(strcmp(x[i],word) == 0)
printf("Found match\n");

== can't be used to compare strings as you are doing it.

This only compares the pointers and not the strings

Upvotes: 8

Related Questions