7+ CPP Blank Space Remover Programs

This post contains a total of 7+ CPP Blank Space Remover Program Examples with Source Code. All these Blank Space Remover Programs are used to remove Blank / White Space from Strings, and these programs are made using C++.

You can use the source code of these examples with credits to the original owner.

Related Posts

CPP Blank Space Remover Programs

1. By Mohammad Dakdouk

Made by Mohammad Dakdouk. C++ Program to Remove empty space from a string. Source

# The original text:

    > hello hi

# The text after removing spaces:

    >>> hellohi
#include <iostream>
using namespace std;


string Remove_Space(string s){
    
    int i;
    string res = "";
    
    
    for(i = 0; s[i] != '\0'; i++){
        if(s[i] == ' '){
            continue;
        }else{
             res += s[i];
        }
    } 
    
    return res;
}


int main() {

    int i;
    string text;
    
    //read the text from user as one line
    getline(cin,text);
    
    cout <<"# The original text:\n\n    > ";
    cout << text;
    cout << "\n\n";
    cout << "# The text after removing";
    cout << " spaces:";
    cout << "\n\n    >>> ";
    
    //call the function to update string
    text = Remove_Space(text);
    cout << text;
    
    cout << "\n\n";  
    cout << "Thanks for your ";
    cout << "caring...\n\n          ";  
    cout << "=======================";
    cout << "\n\nRegards,\nMohammad";
    
    return 0;
}

2. By ROHIT KANOJIYA

Made by ROHIT KANOJIYA. Simple Program to remove space from string. Source

You have Entered String: text string

String Without Spaces: textstring
#include<iostream>
#include<cstring>
using namespace std;
 //Enter a string with Spaces.
int main()
{
 char s[100];
 int i;
// cout<<"Enter a String: ";
 cin.getline(s,100);
 
 int l=strlen(s);

 cout<<"You have Entered String: ";
 cout.write(s,l);

 cout<<"\n\nString Without Spaces: ";
 for(i=0;s[i]!='\0';i++)
  {
   if(s[i]!=' ')   
   cout<<s[i];
  }
 return 0;
}

3. By Saurabh Tiwari

Made by Saurabh Tiwari. Very Basic Program to remove space from text and number strings. Source

1 1 1
111

a b c
abc
#include <iostream>
using namespace std;
 //Compiler version g++ 6.3.0
 int main()
 {
 char a[100];
 cin.getline(a,100);
 for(int i=0;a[i]!='\0';i++)
 {
 	a[i]!=' '?cout<<a[i]:cout<<a[++i];
 }
 }

4. By Alexander Malcev

Made by Alexander Malcev. Source

qw er ty
qwerty
#include <iostream>
#include <string>
using namespace std;

int main() {
	string input,output;
	getline(cin,input);
	for (int i = 0; i < input.length(); i++) {
	  if (input[i] != ' ') {
	   output +=  input[i];
	  }
	}
	cout << output;
}

5. By Med Arezki

Made by Med Arezki. Program to remove string spaces and return output without spaces. Source

hi hello
hihello
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{string a;
getline(cin,a);
for(int i(0);i<a.size();i++)
{if(isspace(a[i]))a.erase(i,1);}
cout<<a;
return 0;}

6. By MSE

Made by MSE. Small C++ program to remove white space from strings. You have to enter your own string in “char s[] = “Hello World!” line. Source

Hello World!
HelloWorld!
#include <iostream>
#include <cstring>

using namespace std;

int main() {

	char s[] = "Hello World!";
	for(int i = 0; i < int(strlen(s));++i)
		if(int(s[i]) !=32) cout << s[i];
		
	return 0;
}

7. By Muhammad Hasan

Made by Muhammad Hasan. Source

Before : x y z
After : xyz
#include <iostream>
using namespace std;
int main() {
    string n;
    getline(cin,n);
    cout << "Before : ";
    for(int i = 0; i < n.size(); i++){
        cout << n[i];
    }
    cout << "\nAfter : ";
    for(int i = 0; i < n.size(); i++){
        if(n[i] != 32){cout << n[i];}
    }
    return 0;
}

8. By Muhammad Dony Mulya

Made by Muhammad Dony Mulya. Enter your own string with empty spaces in string x to get the output string without any space. Source

abcdef
#include <iostream>
using namespace std;
int main() {
    string x {"abc def"};
    string y;
    for(auto& currentChar : x) {
        if(currentChar != ' ')
            y += currentChar;
    }
    
    cout << y;
    return 0;
}