2

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!

6 Answers 6

8

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

Sign up to request clarification or add additional context in comments.

Comments

6

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.

2 Comments

Thanks bro! Would you say it's a good way to implement a string list using a 2D array?
@EmirIsakovic; Depends on situations. In your case, it is fine.
4

This

if (x[i] == word)

should be

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

Comments

1

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.

Comments

0

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");

Comments

-3

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;
}

2 Comments

While the advice would be good for C++, this question is tagged as a C exercise, where string is not valid. It's best to refrain from using C++ functionality in a C question, as the OP may be restricted to C for reasons beyond his/her control.
The OP tagged this as c. You provided a c++ solution which is not available in c.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.