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
    t - empty string
    wasLastSpace = false
    for each character c in s

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

           wasLastSpace = (c = " ")


    return t



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)

                wasLastSpace = (c == ' ');


            return result.ToString();


        static void Main(string[] args)

            string line = ReadLine();

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


        static string ReadLine()
            Console.Write("Text to process (ENTER to quit): ");
            return Console.ReadLine();



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):

If you wish to learn more, please watch my latest video courses


Zoran Horvat

Zoran Horvat is the Principal Consultant at Coding Helmet, speaker and author of 100+ articles, and independent trainer on .NET technology stack. He can often be found speaking at conferences and user groups, promoting object-oriented and functional development style and clean coding practices and techniques that improve longevity of complex business applications.

  1. Pluralsight
  2. Udemy
  3. Twitter
  4. YouTube
  5. LinkedIn
  6. GitHub