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:

  1. Take a string input.
  2. We run a loop, character by character to find the null/white space.
  3. In the loop, we check the presence of null character, when encountered, we increment the index.
  4. Next, we input the remaining characters in the new string, newstr.
  5. 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?

Was this helpful?

Your feedback shapes better tutorials for everyone.