Marcin Kostrzewa
Marcin Kostrzewa

Reputation: 595

How can I get a word from string

I have f.e. "I am working as a nurse." How Can I or which function use to get word from letter number 1 to space or to letter number 11?

So should I get " am working "

Upvotes: 1

Views: 20194

Answers (3)

Joseph Mansfield
Joseph Mansfield

Reputation: 110648

It is not totally clear what you want, but from your example it seems like you want the substring that starts at character 1 and ends on the character 11 places later (that's 12 characters total). That means you want string::substr:

std::string str("I am working as a nurse");
std::string sub = str.substr(1,12);

Upvotes: 2

tletnes
tletnes

Reputation: 1998

char[] source = "I am working as a nurse."
char[11] dest;
strncpy(dest, &source[1], 10)

Upvotes: 0

Loki Astari
Loki Astari

Reputation: 264331

To read a word from a stream use operator>> on a string

std::stringstream  stream("I am working as a nurse.");
std::string  word;

stream >> word;  // word now holds 'I'
stream >> word;  // word now holds 'am'
stream >> word;  // word now holds 'working'
// .. etc

Upvotes: 9

Related Questions