by Zoran Horvat
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.
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
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();
}
}
}
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
In this course, you will learn the basic principles of object-oriented programming, and then learn how to apply those principles to construct an operational and correct code using the C# programming language and .NET.
As the course progresses, you will learn such programming concepts as objects, method resolution, polymorphism, object composition, class inheritance, object substitution, etc., but also the basic principles of object-oriented design and even project management, such as abstraction, dependency injection, open-closed principle, tell don't ask principle, the principles of agile software development and many more.
More...
In this course, you will learn how design patterns can be applied to make code better: flexible, short, readable.
You will learn how to decide when and which pattern to apply by formally analyzing the need to flex around specific axis.
More...
This course begins with examination of a realistic application, which is poorly factored and doesn't incorporate design patterns. It is nearly impossible to maintain and develop this application further, due to its poor structure and design.
As demonstration after demonstration will unfold, we will refactor this entire application, fitting many design patterns into place almost without effort. By the end of the course, you will know how code refactoring and design patterns can operate together, and help each other create great design.
More...
In four and a half hours of this course, you will learn how to control design of classes, design of complex algorithms, and how to recognize and implement data structures.
After completing this course, you will know how to develop a large and complex domain model, which you will be able to maintain and extend further. And, not to forget, the model you develop in this way will be correct and free of bugs.
More...
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.