nullVoid
nullVoid

Reputation: 197

How can you compare words within a string array?

Basically I want to check a string array to see if any of the words match "and".

  1. Is this possible?
  2. Can you push me in the right direction?

Thanks

I should make it clear that the words are char put together best way to explain is an example

abc defg hijk and lmnop <-- each character is in its own element

Upvotes: 0

Views: 2131

Answers (3)

Leo
Leo

Reputation: 1223

I am guessing you'd like to take care of lower\upper casing and the word appearing in the beginning\end of the string.

std::string data;
std::transform(data.begin(), data.end(), data.begin(), ::tolower);
data.append(' ');
if (data.find("and ") != std::string::npos) ......

Upvotes: 0

Fred Larson
Fred Larson

Reputation: 62063

I recommend you use std::string and not null-terminated char* strings (maybe you already are -- hard to be sure). And use a standard container rather than an array. Then use std::find (which would work on an array too, but containers are better).

Upvotes: 2

Luchian Grigore
Luchian Grigore

Reputation: 258568

Iterate through the array and use int string::compare ( const string& str ) const; to check for matches.

Break from the loop on first match.

Upvotes: 1

Related Questions