separating a string

I am trying to figure out how to take a string and only get the back portion of it. The string holds an id number and a first and last name. How would I go about taking only the last name?
For example
string s="38452554 John Smith";
how do i get a string t="John Smith" or a string u=Smith;

Thanks for your help
1
2
3
4
5
6
7
8
9
10
11
12
#include	<string>
#include	<iostream>
#include	<algorithm>
#include	<cctype>


int main()
{
	std::string s( "38452554 John Smith" );

	std::cout << std::string( std::find_if( s.begin(), s.end(), isalpha ), s.end() ) << std::endl;
}


Or you can search a space inside the string and extruct a word.
Last edited on
closed account (o3hC5Di1)
Hi there,

Have a look at this page: http://cplusplus.com/reference/string/string/
Near the bottom there are functions to find contents in strings and to get substrings from them.

The find_() family of functions and substr() should work for your prupose I think.

Hope that helps.
All the best,
NwN
Thanks everyone I found another way to do it as well.

#include <sstream>
#include <string>
#include <vector>
using namespace std;

stringstream os;
string temp;
temp=(*name)[x];
os<<temp;
string id;
os>>id;
string fname;
os>>fname;
string lname;
os>>lname;
Topic archived. No new replies allowed.