Remove All Whitespace

Published on 12 May 2022 (Updated: 14 November 2024)

Welcome to the Remove All Whitespace page! Here, you'll find a description of the project as well as a list of sample programs written in various languages.

This article was written by:

Description

A string is a collection of characters. Sometimes strings contain whitespace characters like " ", "\t", and "\n". The purpose of this project is to remove all such spaces from a string. For example, given the string "Remove All Whitespace", we could generate a new string with all the whitespace removed as follows: "RemoveAllWhitespace". In this case, two spaces were removed at positions 6 and 10.

For simplicity, we will be restricting the types of whitespace to these four types of characters: spaces (" "), tabs ("\t"), newlines ("\n"), and carriage returns ("\r"). We are aware that other types of whitespace exist. That said, programs in this collection tend to be simple, so we can replicate them in as many programming languages as possible.

Requirements

To satisfy the requirements, a program must accept a string on the command line and return a new string with all spaces removed as follows:

$ remove-all-whitespace.lang "   Hello, World!   "
$ "Hello,World!"

In this case, we start with a string that has leading, trailing, and inner spaces. Ultimately, we want to return a string with all of the spaces removed.

Testing

Every project in the Sample Programs repo should be tested. In this section, we specify the set of tests specific to Remove All Whitespace. In order to keep things simple, we split up the testing as follows:

Remove All Whitespace Valid Tests

Description Input
Sample Input: No Spaces "RemoveAllWhitespace"
Sample Input: Leading Spaces " RemoveAllWhitespace"
Sample Input: Trailing Spaces "RemoveAllWhitespace "
Sample Input: Inner Spaces "Remove All Whitespace"
Sample Input: Tabs "\tRemove\tAll\tWhitespace\t"
Sample Input: Newlines "\nRemove\nAll\nWhitespace\n"
Sample Input: Carriage Returns "\rRemove\rAll\rWhitespace\r"

All of these tests should output the following:

RemoveAllWhitespace

Remove All Whitespace Invalid Tests

Description Input
No Input  
Empty Input ""

All of these tests should output the following:

Usage: please provide a string

Articles

There are 16 articles: