# Removing Multiple Consecutive Spaces from a String

by Zoran Horvat

## Problem Statement

Given a string, write a function which replaces multiple white space characters with single space. Function should return the modified string.

Example: If string " yet another sunny day " is passed to the function, it should return string " yet another sunny day ". Note that heading and trailing space characters have remained.

## Problem Analysis

In this exercise we are dealing with character strings. Despite the possible view that they are representing words and sentences, strings are not much more than common arrays of characters.

We are asked to remove multiple spaces and replace them with exactly one space character each. Starting from the stand that strings are arrays of characters, we could reformulate the problem statement into a more convenient form.

Instead of insisting on replacing the spaces, we could say that in each group of consecutive spaces all spaces except one should be removed from the string. This immediately gives the idea that we could walk through the string and count consecutive spaces. Each time a non-space character is encountered, the counter is reset to zero. Repeated spaces will be discovered by observing their associated count, which indicates their position within the current group and will always be greater than one. All such spaces will be skipped, which means that only the first space in each group will be pushed to the output.

Now that we have formulated the solution in this way, it is obvious that we do not really need the counter. It is sufficient to just keep a Boolean flag which would indicate whether the space character has been pushed to the output or not. Here is the pseudocode of the function which performs this transformation:

``````function RemoveMultipleSpaces(s)
s - string
begin
t - empty string
wasLastSpace = false
for each character c in s
begin

if c <> " " OR NOT wasLastSpace then
append c to t

wasLastSpace = (c = " ")

end

return t

end
``````

## Implementation

Code below is a C# console application which implements the function for removing multiple spaces from the string and lets the user enter texts to process.

``````using System;

namespace RemovingMultipleSpaces
{

public class Program
{

static string RemoveMultipleSpaces(string s)
{

StringBuilder result = new StringBuilder();
bool wasLastSpace = false;

for (int i = 0; i < s.Length; i++)
{

char c = s[i];

if (c != ' ' || !wasLastSpace)
result.Append(c);

wasLastSpace = (c == ' ');

}

return result.ToString();

}

static void Main(string[] args)
{

while (line.Length > 0)
{
string modified = RemoveMultipleSpaces(line);
Console.WriteLine("Modified text: \"" + modified + "\"");
Console.WriteLine();
}

}

{
Console.Write("Text to process (ENTER to quit): ");
}

}
}
``````

## Demonstration

When console application is run, it produces output like this:

```            ```
Text to process (ENTER to quit): removing    multiple spaces    is    so simple
Modified text: "removing multiple spaces is so simple"

Text to process (ENTER to quit):    yet     another sunny     day
Modified text: " yet another sunny day "

Text to process (ENTER to quit):
```
```