Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
GreenGeeks Web Hosting - Faster, Scalable & Eco-Friendly

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


8,090 questions

11,268 answers

573 users

How to trim trailing spaces from string in C++

1 Answer

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

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

static inline string rtrim(string &s) {
	size_t endpos = s.find_last_not_of(" \t");
	size_t startpos = s.find_first_not_of(" \t");
	if (string::npos != endpos)
	{
		s = s.substr(0, endpos + 1);
		s = s.substr(startpos);
	}

	return s;
}

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

	cout << rtrim(s) << endl;
	cout << s << endl;

	return 0;
}


/*
run:

c++ c c# java php
c++ c c# java php

*/

 




answered Apr 19 by avibootz
...