How to assign part of a string to other string in C++

2 Answers

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

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

int main()
{
	string s1 = "c c++ java php", s2;
	
	s2.assign(s1, 2, 3);

	cout << s2 << endl;

	return 0;
}


/*
run:

c++

*/

 



answered May 29, 2018 by avibootz
0 votes
#include <iostream>
#include <string>

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

int main()
{
	string s1 = "c c++ java php", s2;
	
	int index = s1.find("c++");
	if (index != string::npos) {
		s2.assign(s1, index, 3);
		cout << s2 << endl;
	}

	return 0;
}


/*
run:

c++

*/

 



answered May 29, 2018 by avibootz

Related questions

1 answer 171 views
1 answer 221 views
1 answer 136 views
2 answers 195 views
1 answer 173 views
1 answer 183 views
1 answer 126 views
...