Wear a helmet. Even when coding.

exercises > removing-multiple-spaces

Exercise #23: Removing Multiple Consecutive Spaces from a String
by Zoran Horvat @zoranh75
February 15, 2014

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

See also:

Published: Feb 15, 2014; Modified: Feb 17, 2014


Zoran is software architect dedicated to clean design and CTO in a growing software company. Since 2014 Zoran is an author at Pluralsight where he is preparing a series of courses on object-oriented and functional design, design patterns, writing unit and integration tests and applying methods to improve code design and long-term maintainability.

Follow him on Twitter @zoranh75 to receive updates and links to new articles.

Watch Zoran's video courses at (requires registration):

Making Your C# Code More Object-Oriented

This course will help leverage your conceptual understanding to produce proper object-oriented code, where objects will completely replace procedural code for the sake of flexibility and maintainability. More...

Advanced Defensive Programming Techniques

This course will lead you step by step through the process of developing defensive design practices, which can substitute common defensive coding, for the better of software design and implementation. More...

Tactical Design Patterns in .NET: Creating Objects

This course sheds light on issues that arise when implementing creational design patterns and then provides practical solutions that will make our code easier to write and more stable when running. More...

Tactical Design Patterns in .NET: Managing Responsibilities

Applying a design pattern to a real-world problem is not as straight-forward as literature implicitly tells us. It is a more engaged process. This course gives an insight to tactical decisions we need to make when applying design patterns that have to do with separating and implementing class responsibilities. More...

Tactical Design Patterns in .NET: Control Flow

Improve your skills in writing simpler and safer code by applying coding practices and design patterns that are affecting control flow. More...

Writing Highly Maintainable Unit Tests

This course will teach you how to develop maintainable and sustainable tests as your production code grows and develops. More...

Improving Testability Through Design

This course tackles the issues of designing a complex application so that it can be covered with high quality tests. More...

Share this article