Linear Search in Char Array - C ++ (Visual Studio 2005)

I am very new to C ++ programming and you can see why.

I want to create a character array consisting of multiple words that I want to perform with a linear search function. Does this array have to be a two dimensional array? For instance:

char Colors[3][6] = {"red", "green", "blue"};

      

I tried like this:

char Colors[] = {"red", "green", "blue"};

      

This gave me the error "too many initializers".

I assume the 1st method is correct as it contains the number of elements in the array and the maximum length of the element, right?

Now, how would I implement a linear search function to find a word within this array? Can I do something like the following:

(Assuming linearSearch function is already declared)

char searchKey;  
char element;

char Colors[3][6] = {"red", "green", "blue"};

printf("Enter the color to look for: \n");

scanf("%s", searchKey);

element = linearSearch(Colors, searchKey, ??); //?? is where I don't know what to enter

if (element != -1)  
{  
    printf("Found the word.\n");  
}  
else  
{  
    printf("Didn't find the word.\n");  
}

      

Is it possible? If so, what would the declaration look for for the linearSearch function? Hopefully I've provided enough information to make this somewhat helpful.

edit: Thanks everyone for the help, ran the program as intended.

+1


a source to share


6 answers


You can force the linearSearch function to return the index of the search term into an array. Here's a sample program:

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

int linearSearch (const char **Array, const char *searchKey, int arraySize) {
    for (int i = 0; i < arraySize; ++i) {
        if (strcmp(Array[i], searchKey) == 0)
            return i;
    }

    // We didn't find the searchKey in the Array
    return -1;
}

int main () {
    char *colors[] = { "red", "green", "blue" };

    int index = linearSearch (colors, "green", 3);

    if (index < 0) { // search string was not found
        printf("Search string not found!\n");
    }
    else {
        printf("String %s found at index %d\n", colors[index], index);
    }

    return 0;
}

      



We use the strcmp () function to compare strings. It returns zero if the strings match and are nonzero if they don't. To use it, you need to include a header string.h

.

But as others have suggested, you should use the STL if you can.

+2


a source


I would suggest learning about the C ++ standard library which helped you a lot. For instance,

#include <vector>
#include <string>
#include <algorithm>
#include <iostream>

using namespace std;

vector<string> words;
words.push_back("red");
words.push_back("blue");
words.push_back("green");

if (find(words.begin(), words.end(), "green") != words.end())
    cout << "found green!"
else
    cout << "didn't find it";

      



Why implement it linearSearch

yourself? C ++ already has std::find

one that does it for you! Moreover, if you use set

instead vector

, you can now use std::binary_search

, which is O (log n) rather than O (n), since the set is sorted.

+9


a source


To declare an array of strings use this syntax

 char *Colors[] = {"red", "green", "blue"};

      

It is an array of pointers to characters ("Hello" evaluates to a const char * pointing to "H"). The compiler will figure out how many elements it takes to store your array (hence []), in which case it will always be 3.

In general, I agree with rlbond's answer - you must use STL.

+3


a source


No, you don't need a two dimensional array.

Here's a way to declare string arrays:

char* Colors[3] = {"red", "green", "blue"};

      

or

char* Colors[] = {"red", "green", "blue"}; // the compiler will figure out the size

int colors = sizeof(Colors)/sizeof(Colors[0]);

      

Right after experimenting with C ++, you should learn how to use STL.

+2


a source


This article contains a string search function. It should also give you an idea of ​​how to properly structure character arrays.

0


a source


If you don't want to use strings and are left with char arrays, you can use strcmp to compare 2 words. The thing to remember with strcmp is that it returns the index of where the word was found, so if you only want to find words at the beginning, you do it like this:

for(int i=0;i<SizeOfColorArray;i++)
{
    if(strcmp (MySearchTerm,colors[i]) == 0)
    {
        // it was  a match
        return i;
    }
}

      

Depending on what you are doing and how large your array is, you should consider using string hashes to increase pre-availability.

0


a source







All Articles