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”

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
Note
Time complexity of above solution is O(n) and it does only one traversal of string.
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
#include
using namespace std;
void reverse(string &s,int f,int l)
{
while(f<=l)
{
swap(s[f++],s[l–]);
}
}
int revWord(string &s,int n)
{
int start =0;
for(int i=0;i<n;i++)
{
if(s[i]==' ')
{
reverse(s,start,i-1);
start = i+1;
}
}
reverse(s,start,n-1);
}
int length(string s)
{
int length =0;
for(int i=0;s[i]!='\0';i++)
{
length++;
}
return length;
}
int main()
{
string s;
getline(cin,s);
int len =length(s);
revWord(s,len);
cout<<s;
return 0;
}
#include
#include
using namespace std;
int main(){
char str[50];
cout<<"Enter string: ";
gets(str);
int len=strlen(str);
int i, j;
for(i=0; str[i]!='\0'; i++){
if(str[i]==' '){
for(j=i; str[j]!='\0'; j++){
str[j] = str[j+1];
len–;
}
}
}
cout<<"String After removing the space: "<<str;
}
#include
#include
using namespace std;
int main(){
char str[50];
cout<<"Enter string: ";
gets(str);
int len=strlen(str);
int i, j;
for(i=0; str[i]!='\0'; i++){
if(str[i]==' '){
for(j=i; str[j]!='\0'; j++){
str[j] = str[j+1];
len–;
}
}
}
cout<<"String After removing the space: "<<str;
}
#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;
}
#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;
}
#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=”)