finding first word having a particular char 'w' in a sentence

how can i find the first word in my sentence having 'w' character. This character can be present anywhere in my word. Lets take an example of sentence "Hi xyzwy! what are you doing here?
So the result should be "xyzwy".
How much code have you written ?
hi bluecoder,

this is what i have written

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>

int main() {
using namespace std;
string sentence = "Hi xyzwy! what are you doing here?";
istringstream iss(sentence);
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
ostream_iterator<string>(cout, "\n"));

}

but i dnt know how to search for w now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <sstream>

int main() {
	using namespace std;
	string sentence = "Hi xyzwy! what are you doing here?";
	string word;
	istringstream iss(sentence);
	while (iss >> word)
		if (string::npos != word.find('w'))
			break;

	cout << "Found first word with containing 'w': " << word << endl;
	return 0;
}


Questions?

Maikel
Last edited on
@maikel:

Don't just post code, it doesn't help the OP learn anything.
It seems to me, that he already know a lot and just needed the correct usage of find and >> operator..

Do you think he is beginner? I'm sorry then.

Maikel
@maikel

thanks a lot maikel, i was trying so many things from 2 hours but nothing worked.

@firedraco

i know C++ already. was trying different logics but nothing worked.
i'm sorry if i did anything wrong. :-(
Topic archived. No new replies allowed.