Remove All Blank Spaces in a String

Remove All Blank Spaces in a String” is  again a basic problem of string data structure. Here, we are given a string of length ‘n’ and our task is to remove the blank spaces from the string.

Example:

INPUT:
Help Me Study Bro
OUTPUT:
HelpMeStudyBro

The steps required to Remove All Blank Spaces in a String are as follows:

  1. Iterate the string and check whether the character is blank space or not.
  2. If the character is blank space, then move all the subsequent characters one position back. Then delete the last character of the string.
  3. If the character is not blank space character, continue iterating the string.

C++ Program to Remove All Blank Spaces in a String is as follows:

/* Program to Remove All Blank Spaces in a String */
#include<bits/stdc++.h>
#define size 1000  
using namespace std;  
int main()  
{  
    char str[size];  
    cout<<"Enter a string:\n";  
    cin.getline(str,size);  
    cout<<"Before Removing Spaces:\n";  
    cout<<str;  
    /* Iterate over the complete string */  
    for(int i = 0; i < size && str[i] != '
 /* Program to Remove All Blank Spaces in a String */ #include<bits/stdc++.h> #define size 1000 using namespace std; int main() { char str[size]; cout<<"Enter a string:\n"; cin.getline(str,size); cout<<"Before Removing Spaces:\n"; cout<<str; /* Iterate over the complete string */ for(int i = 0; i < size && str[i] != '\0'; i++) { /* If character is blank space, move the subsequent character one position back */ if(str[i] == ' ') { int j; for(j = i; j < size && str[j] != '\0'; j++) { str[j] = str[j+1]; } /* Remove the last character, by putting NULL character in the last*/ str[j-1] = '\0'; } } cout<<"\nAfter Removing Spaces:\n"; cout<<str; } 
'; i++) { /* If character is blank space, move the subsequent character one position back */ if(str[i] == ' ') { int j; for(j = i; j < size && str[j] != '
 /* Program to Remove All Blank Spaces in a String */ #include<bits/stdc++.h> #define size 1000 using namespace std; int main() { char str[size]; cout<<"Enter a string:\n"; cin.getline(str,size); cout<<"Before Removing Spaces:\n"; cout<<str; /* Iterate over the complete string */ for(int i = 0; i < size && str[i] != '\0'; i++) { /* If character is blank space, move the subsequent character one position back */ if(str[i] == ' ') { int j; for(j = i; j < size && str[j] != '\0'; j++) { str[j] = str[j+1]; } /* Remove the last character, by putting NULL character in the last*/ str[j-1] = '\0'; } } cout<<"\nAfter Removing Spaces:\n"; cout<<str; } 
'; j++) { str[j] = str[j+1]; } /* Remove the last character, by putting NULL character in the last*/ str[j-1] = '
 /* Program to Remove All Blank Spaces in a String */ #include<bits/stdc++.h> #define size 1000 using namespace std; int main() { char str[size]; cout<<"Enter a string:\n"; cin.getline(str,size); cout<<"Before Removing Spaces:\n"; cout<<str; /* Iterate over the complete string */ for(int i = 0; i < size && str[i] != '\0'; i++) { /* If character is blank space, move the subsequent character one position back */ if(str[i] == ' ') { int j; for(j = i; j < size && str[j] != '\0'; j++) { str[j] = str[j+1]; } /* Remove the last character, by putting NULL character in the last*/ str[j-1] = '\0'; } } cout<<"\nAfter Removing Spaces:\n"; cout<<str; } 
'; } } cout<<"\nAfter Removing Spaces:\n"; cout<<str; }
OUTPUT:
Enter a string: Help Me Study Bro
Before Removing Spaces: Help Me Study Bro
After Removing Spaces: HelpMeStudyBro
Time Complexity:
O(n^2)

C++ Program to Remove All Blank Spaces in a String is as follows:

/* Program to Remove All Blank Spaces in a String */
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    string str;  
    cout<<"Enter a string:\n";  
    getline(cin,str);  
    cout<<"\nBefore Removing Spaces: ";  
    cout<<str;  
     /* Iterate over the complete string */  
    for(int i = 0; i <  str.length(); i++)  
    {  
        if(str[i] == ' ')  
        {  
           /* If character is blank space, move the  
           subsequent character one position back */  
            for(int j = i; j <  str.length(); j++)  
            {  
                str[j] = str[j+1];  
            }  
            /* Remove the last character, by  
             popping out last character of the string*/  
            str.pop_back();  
        }  
    }  
    cout<<"\nAfter Removing Spaces: ";  
    cout<<str;  
}  
OUTPUT:
Enter a string: Help Me Study Bro
Before Removing Spaces: Help Me Study Bro
After Removing Spaces: HelpMeStudyBro
Time Complexity:
O(n^2)

Related Posts:

  1. Program to check if String is isogram or not.
  2. Program to Reverse Each word of the String.
  3. Program to Print All the Substring of the Given String.
  4. Program to find longest palindromic Substring.
  5. Program to check Anagram Strings.
  6. Program to check whether two Strings are Rotation of each other or not.
  7. Program to check Palindromic Anagram.
  8. Program to print all the Palindromic Substring of the String.
  9. Program to check Panagram String.
  10. Program to find first non-repeating character of the String.
  11. Program to Reverse a String.
  12. Program to Count Number of Characters in a String.
  13. Program to Count frequency of particular character of the String.
  14. Program to Count Number of Vowels and Number of Consonants in a String.
  15. Program to Check if String is alphabetic or numeric or alpha-numeric.
  16. Program to copy one String to Another.
  17. Program to Concatenate Two Strings.
  18. Program to Check whether given String is Palindrome or not.
  19. Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
  20. Program to count total number of words in a string.

You may also like...