Check if there's a space before a word in a string

Hey,

Is there a nice and easy way to find if there's a space before or after a word in string?

Lets say I have a string like this: "Blue apples are cool"

I know position and length of every word. I was thinking to do something like check if position-1 is a space, but couldn't do this. Any tips ?

Thanks a lot,
I was thinking to do something like check if position-1 is a space, but couldn't do this.
What stopped you from doing that?
My poor programming skills I guess. I'm pretty sure it's not as simple as if (str[i] == ""). I know it's one hell of a n00bish question, but I can't figure it out. So, can you help me ?
if(str[i-1] == " ")?
if(str[i-1] == " ")?


Nah, it doesn't work like that. Either way, I've done it with find_first_of.

Thanks,
Alvyxaz wrote:
if(str[i-1] == " ")?


Nah, it doesn't work like that. Either way, I've done it with find_first_of.


Sure it does. Typo in firedracos code snippet, should be ' ' not " ".

Example:
1
2
3
4
5
	string sentence = "This is a sentence";

	for ( int i = 0; i < sentence.size(); i++ )
		if ( sentence[i] == ' ' )
			cout << "Space found at position " << i << endl;
Last edited on
Topic archived. No new replies allowed.