How to find the last word in a string with C++

1 Answer

0 votes
#include <iostream>
#include <string>

int main()
{
	using namespace std;

	string str = "c++ c c# python java";

	const auto pos = str.find_last_of(" \t\n"); 

	pos == string::npos ? str : str.substr(pos + 1);

	cout << "pos = " << pos << endl;
	cout << str.substr(pos + 1) << endl;

	return 0;
}



/*
run:

pos = 15
java

*/

 



answered Jun 6, 2017 by avibootz
...