http://www.codinghelmet.com/  

Wear a helmet. Even when coding.

exercises > removing-multiple-spaces

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

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

            string line = ReadLine();

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

        }

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

    }
}

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

See also:

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

ZORAN HORVAT

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 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 pluralsight.com (requires registration):

Tactical Design Patterns in .NET: Managing Responsibilities

Applying a design pattern to a real-world problem is not as straightforward as literature implicitly tells us. It is a more engaged process. This course gives an insight into 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...

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

webmasters