C++ program to remove spaces from a string

Removing spaces from a string.

In this article we will look at how to write a C program to remove spaces from a string. 

Here we will store the string in a character array lets say str and that original string will contain the spaces in between.

Example:- 

  • Input string: “Prep  insta”
  • Output string: “Prepinsta”
Cpp program to remove spaces from a string

Algorithm:

  • Initialize the variables and accept the input.
  • Initialize while loop and terminate it at the end of string.
  • Iterate each character through the loop.
  • Store each character except spaces in the new string.
  • Print result.

CPP programming code to remove spaces from a string

Run
#include <iostream>
using namespace std;
// Function to remove all spaces from a given string
void removeSpaces(char *str)
{
// To keep track of non-space character count
int count = 0;
// Traverse the provided string. If the current character is not a space,
//move it to index 'count++'.
for (int i = 0; str[i]; i++)
if (str[i] != ' ')
str[count++] = str[i]; // here count is
// incremented
str[count] = '\0';
}
// Driver program to test above function
int main()
{
char str[] = "P re p i n sta ";
removeSpaces(str);
cout << str;
return 0;
}

Output

Prepinsta

Method 2

Run
#include <iostream> 
#include <algorithm> 
using namespace std;

// Function to remove all spaces from a given string
string removeSpaces(string str)
{
    str.erase(remove(str.begin(), str.end(), ' '), str.end());
    return str;
}
// Driver program to test above function
int main()
{
    string str = "Pr epi  ns  ta ";
    str = removeSpaces(str);
    cout << str;
    return 0;
}

Output

Prepinsta

Method 3

This can also be solved using predefined STL functions like count() ,remove() ,getline() and resize() .

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s = "Pr e pi ns ta";
    cout << "string with spaces is " << s << endl;

    int l = s.length(); // storing the length of the string
    int c= count(s.begin(), s.end(),' '); // counting the number of whitespaces

    remove(s.begin(), s.end(),' '); // removing all the whitespaces
    s.resize(l - c); // resizing the string to l-c

    cout << "string without spaces is " << s << endl;
    return 0;

}

Output

string with spaces is Pr e pi ns ta
string without spaces is Prepinsta

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

3 comments on “C++ program to remove spaces from a string”


  • vishal

    #include
    #include
    using namespace std;
    int main(){
    string str;
    cout<<"enter a string"<<endl;
    getline(cin,str);
    for(int i = 0; str[i] != '\0'; i++)
    {
    if(str[i]!=' ')
    {
    cout<<str[i];
    }
    }
    return 0;

    }


  • shivprasad

    #include
    #include
    #include
    using namespace std;
    int main()
    {
    string s;
    getline(cin,s);
    int length=s.length();
    int i=0;
    while(s[i]!=’\0′)
    {
    if(s[i]==’ ‘)
    {
    for(int j=i;j<length;j++)
    {
    s[j]=s[j+1];
    }
    length–;
    }
    i++;
    }
    cout<<s;
    return 0;
    }


  • Asish

    #python3
    x=input()
    a=[d for d in x]
    b=[]
    for i in range(len(a)):
    if a[i]!=’ ‘:
    b.append(a[i])
    for j in range(len(b)):
    print(b[j],end=”)