C++ Removing spaces from String
Posted by Samath
Last Updated: January 05, 2017

This program removes all the space from a sentence entered by the user.

#include <iostream>
#include <stdio.h>
#include <string.h> 
using namespace std;

string rem_space(string line) {
 while (1){
   int letter = line.find(' ');
   if (letter == -1 ){
     break;
   }
   line = line.erase(letter, 1);
 }
 return line;
}

int main() {
	string sentence;
  cout<<"Please enter a sentence: ";
  getline (cin,sentence);
  string newsentence = rem_space(sentence);
   cout << newsentence << endl;
  return 0;
}

 

Related Content