C++ Program to remove spaces from a string?

The program takes a string and removes the spaces in it. This is useful when we want to save the space of our The following sample shows how it is done with an explanation.

Input: Hello World
Output: HelloWorld


To remove or delete spaces from the string or sentence, you have to ask the user to enter a string. Now start checking for spaces. If space will be found, then start placing the next character from the space to the back until the last character and continue to check for the next space to remove all the spaces present in the string


#include <iostream>
using namespace std;
int  {
   char str[80]="Hello World";
   int i=0, len, j;
   len = strlen(str);
   for( i = 0; i < len; i++) {
      if (str[i] == ' ') {
         for (j = i; j < len; j++)
            str[j] = str[j+1];
   cout << str;
   return 0;

Updated on: 20-Aug-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started