Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,039 questions

52,004 answers

573 users

How to find the last occurrence of character in string with C++

3 Answers

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

using namespace std;

int main()
{
	string s = "c++ c c# java python";

	size_t pos = s.find_last_of("c");

	cout << pos << endl;

	cout << s.substr(0, pos) << endl;
	cout << s.substr(pos + 1) << endl;

	return 0;
}

/*
run:

6
c++ c
# java python

*/

 



answered Feb 25, 2017 by avibootz
0 votes
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string s = "c++ c c# java python";

	size_t pos = s.find_last_of(' ');

	cout << pos << endl;

	cout << s.substr(0, pos) << endl;
	cout << s.substr(pos + 1) << endl;

	return 0;
}

/*
run:

13
c++ c c# java
python

*/

 



answered Feb 25, 2017 by avibootz
0 votes
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int main()
{
	string s = "c c++ java php";
	
	int index = s.rfind("p");

	if (index != string::npos) 
		cout << "found at index: " << index << endl;
	else
		cout << "not found" << endl;

	return 0;
}


/*
run:

found at index: 13

*/

 



answered May 29, 2018 by avibootz

Related questions

1 answer 129 views
1 answer 116 views
2 answers 169 views
2 answers 202 views
1 answer 130 views
...