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:
- Iterate the string and check whether the character is blank space or not.
- If the character is blank space, then move all the subsequent characters one position back. Then delete the last character of the string.
- 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:
- Program to check if String is isogram or not.
- Program to Reverse Each word of the String.
- Program to Print All the Substring of the Given String.
- Program to find longest palindromic Substring.
- Program to check Anagram Strings.
- Program to check whether two Strings are Rotation of each other or not.
- Program to check Palindromic Anagram.
- Program to print all the Palindromic Substring of the String.
- Program to check Panagram String.
- Program to find first non-repeating character of the String.
- Program to Reverse a String.
- Program to Count Number of Characters in a String.
- Program to Count frequency of particular character of the String.
- Program to Count Number of Vowels and Number of Consonants in a String.
- Program to Check if String is alphabetic or numeric or alpha-numeric.
- Program to copy one String to Another.
- Program to Concatenate Two Strings.
- Program to Check whether given String is Palindrome or not.
- Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
- Program to count total number of words in a string.