-1

For a school assignment I need to check whether a string entered by the user is stored in a pre-defined word array.

I want to implement a function to perform the check, that may look like this:

bool exists(dict words, char check) { /* how to implement this? */ }

But I have no clue whether this will work or how to implement it. Can anyone help?

Here's my code:

#include <iostream>
#include <string>

using namespace std;

struct dict {
    string word;
};

int main() {
    dict words[5];
    words[0].word = 'abc';
    words[1].word = 'bcd';
    words[2].word = 'cde';
    words[3].word = 'def';
    words[4].word = 'efg';

    char user_input[100];
    cin.getline(user_input, 100);
    if (...) { // how do I check if the user input is in my word array?
        cout << "found\n";
    }
    else {
        cout << "not found\n";
    }
}
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
code maniac
  • 37
  • 1
  • 5
  • 3
    ... and string literals. – WhozCraig May 06 '15 at 06:26
  • ... and how to ask (in Stack Overflow) – Gaurav Dave May 06 '15 at 06:27
  • 2
    This might be a school assignment, but if not there are better data structures to store strings, like e.g. plain [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) or [`std::array`](http://en.cppreference.com/w/cpp/container/array). Then it will also be very easy to find specific "words" using [`std::find`](http://en.cppreference.com/w/cpp/algorithm/find). Right now you can use [`std::find_if`](http://en.cppreference.com/w/cpp/algorithm/find), but it's a little more complicated. – Some programmer dude May 06 '15 at 06:27
  • yes this is part of my school assignment. – code maniac May 06 '15 at 06:31
  • 2
    You might also want to read about [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline), as then you don't have to worry about plain `char` arrays and sizes. – Some programmer dude May 06 '15 at 06:31

2 Answers2

1

First of all, dict is a structure and char is type able to hold single character, so you would rather need to have:

bool exists(const dict* words, const string& check);

From this point, I would say, that:

But since it's a school assignment, I suppose, that you have some limitations (and can't use neither std::vector nor std::find, that would do the job). So:

bool exists(const dict* words, size_t count, const std::string& check)
{
    for(size_t n = 0; words && (n < count); ++n)
    {
        if(words[n].word == check)
            return true;
    }

    return false;
}

Example:

dict langs[3];

langs[0].word = "C++";
langs[1].word = "Java";
langs[2].word = "Python";

std::string s_1 = "Java";
std::string s_2 = "C++ 11";

printf("exists(%s) : %s\n", s_1.c_str(), exists(langs, 3, s_1) ? "yes" : "no");
printf("exists(%s) : %s\n", s_2.c_str(), exists(langs, 3, s_2) ? "yes" : "no");

Output:

exists(Java) : yes
exists(C++ 11) : no

Link to sample code.

Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49
1

As the other answer has already pointed out, you should add a size parameter to the function signature in order to be able to iterate the array (especially to know when to stop iteration.). Then a simple loop with a comparison will do the trick.

Note that you shouldn't normally need to use raw arrays in C++, but rather one of the containers from the standard library, e.g., std::vector. Also, you should use std::string and std::getline() for your user input, and you should fix your string literals (use double quotes "..." instead of single quotes '...'). Further, you should avoid using namespace std; conciouslessly. Have a look at the links at the end of this post for some further reading on these points.

Example code:

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

bool exists(std::string const & user_input, 
            std::vector<std::string> const & words)
{
    for (int i = 0; i < words.size(); i++)
        if (user_input == words[i])        
            return true;
    return false;
}

int main() {
    std::vector<std::string> words(5);
    words[0] = "abc";
    words[1] = "bcd";
    words[2] = "cde";
    words[3] = "def";
    words[4] = "efg";

    std::string user_input;
    std::getline(std::cin, user_input);
    if (exists(user_input, words))
        std::cout << "found\n";
    else
        std::cout << "not found\n";
}

Example output:

$ g++ test.cc && echo "abc" | ./a.out
found

The following might be beyond the scope of your school assignment, but maybe this will be helpful for future visitors to this question.

Note that an array (which std::vector is) is not the most efficient data structure to perform this sort of task, as you have to iterate the entire array to check every single item (linear complexity).

The C++ standard library also provides the container types std::set and std::unordered_set (the latter since C++11). Here the search space is organized in a special way (binary search tree: logarithmic complexity, hash table: constant complexity on average) to improve lookup time of the key type (std::string in this case).

Here's an example:

#include <iostream>
#include <string>
#include <set>

typedef std::set<std::string> set_type;

bool input_exists(std::string input, set_type const & words) {
    return words.find(input) != words.end();
}

int main() {
    set_type words = {"abc", "bcd", "cde", "def", "efg"};
    std::string input;
    if (std::getline(std::cin, input)) {
        std::cout << "input: '" << input << "' ";
        if (input_exists(input, words))
            std::cout << "found\n";
        else
            std::cout << "not found\n";
    }
}

Example output:

$ g++ test.cc -std=c++11
$ echo "abc" | ./a.out
input: 'abc' found
$ echo "abcdefg" | ./a.out
input: 'abcdefg' not found

For reference:

Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187