C++ Program to remove spaces from a string?

C++Server Side ProgrammingProgramming

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

Explanation

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

Example

#include <iostream>
#include<string.h>
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];
            len--;
      }
   }
   cout << str;
   return 0;
}
raja
Published on 20-Aug-2019 12:32:02
Advertisements