TutorialStudyMite
Program to remove whitespaces from string in C++
JJuhi Kamdar1 min read
Beginner friendly
Track completion, mastery, and revision.
Logic:
In this method, we find out all nulls and ignore all the nulls in a string and store the remaining contents in another string.
Algorithm:
- Take a string input.
- We run a loop, character by character to find the null/white space.
- In the loop, we check the presence of null character, when encountered, we increment the index.
- Next, we input the remaining characters in the new string, newstr.
- Output the new string.
Code:
//removing blank space
#include <iostream>
using namespace std;
int main()
{
string str;
cout<<"Enter the string ";
getline(cin,str);
int len=str.length();
char newstr[len];
}Output:
Enter the string: This Is A String Which Does Not Have Space!
String after removal of blank spaces is:ThisIsAStringWhichDoesNotHaveSpace!Finished reading?