Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c++ string manipulation

Status
Not open for further replies.

5319

Programmer
Jul 9, 2002
2
US
i declared the string:

string = "Firstname middlename lastname"

how do i use standard template library and just print the middle name?

reply back,
thanks
 
Here is some code that will work. It is not as simple as calling one function.

std::string str = "first middle last";

unsigned int first;
unsinged int last;
first = str.find(' ');
last = str.rfind(' ');

std::copy(str.begin()+first+1, str.begin()+last,
ostream_iterator<int>(cout));

Hope that makes sense...
The copy sends the chars to cout. It starts from m and ends right before the space. find gets the ' ' starting from the beginning. r find gets the ' ' starting from the end.
-Skatanic
-Skatanic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top