-1

I've found a couple answers for how to check if a string is a full word, but nothing for how to check if a string is the first part of a word.

For example, I need a function that will return true for "c", "b", "cong" (congratulations, congruent, etc.), or "exa" (example, examine, etc.), but false for "congx", "qt", or other gibberish.

I'd imagine there are a number of ways to go about this. If you could provide a rough outline for a strategy, I would really appreciate it. I'm trying to make a Boggle solver. Thanks!

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
elliott
  • 9
  • 2

1 Answers1

0

Is std::string part contained in std::string word?

if(word.find(part) != std::string::npos)
{
    // part is in word somethere
}

Is std::string part at the beginning of std::string word?

if(word.find(part) == 0)
{
    // word starts with part
}

By way of explanation, std::string::find() returns the position of what was found. So 0 means it was found at the beginning. The value std::string::npos is a special value that indicates nothing was found at all.

Galik
  • 47,303
  • 4
  • 80
  • 117
  • 3
    For _every_ word in a dictionary? – Lol4t0 Apr 21 '15 at 20:24
  • @Lol4t0 As far as I am aware these functions will work for *every* word in the dictionary but I must confess that I have not checked all of them. – Galik Apr 21 '15 at 20:31
  • Thanks, perhaps I can get clever with this so that it doesn't take forever. – elliott Apr 21 '15 at 20:32
  • @Lol4t0 Ah well *now* I understand you. Maybe I misunderstood the question? I thought it was about comparing strings rather than strategies for searching a dictionary. – Galik Apr 21 '15 at 20:36
  • @ElliottShugerman Well for something like this I would turn to a dedicated dictionary library like [GNU Aspell](http://aspell.net/). – Galik Apr 21 '15 at 20:38
  • Well, my believe was that task is to check, if there is an _valid english word_, that starts with the given prefix. – Lol4t0 Apr 21 '15 at 20:40