Search⌘ K
AI Features

Remove White Spaces from a String

Explore how to remove all white spaces from a null-terminated string by applying a two-pointer technique. Understand how read and write pointers work together to skip spaces and build a continuous string, optimizing the process with linear time and constant space complexity. This lesson helps you grasp essential string manipulation skills useful for coding interviews.

Statement

Given a null-terminated string, remove all the white spaces (tabs or spaces) present in the string.

Example:

Sample input

All greek to me.    

Expected output

Allgreektome.

Let’s visualize this below:

Try it yourself #

#include <iostream>
#include <string>
using namespace std;
string RemoveWhiteSpaces(char * str) {
//TODO: Write - Your - Code
string s = str;
return s;
}

Solution

This problem can be solved with two pointers. Here is an overview:

  • Initialize read and write pointers to the start of the string.
  • With the
...