How to erase (delete) the last character from a string in C++

2 Answers

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

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

int main()
{
	string s("c++ programming");

	s.erase(s.end() - 1);

	cout << s << endl;

	return 0;
}

/*
run:

c++ programmin

*/

 



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

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

int main()
{
	string s("c++ programming");

	s = s.substr(0, s.size() - 1);

	cout << s << endl;

	return 0;
}

/*
run:

c++ programmin

*/

 



answered May 9, 2018 by avibootz

Related questions

1 answer 123 views
1 answer 143 views
1 answer 116 views
2 answers 141 views
1 answer 140 views
2 answers 203 views
...