4.5 Remove whitespaces from a given string in Go
4. Basic String Algorithm Problems in Go
Full Course on Data Structures and Algorithms in Go
By: Chrysanthus Date Published: 3 Feb 2026
The reader is advised to read all the lessons (tutorials) in this full course, in the order presented.
You are given a string. You are asked to: A) remove all the spaces created by pressing the keyboard space-bar, and B) remove all the white-space characters.
You are asked to use O(N) time and O(N) space for each problem.
A) Removing all Keyboard Space-bar Characters
This is for the ' ' character. For an input of
"Broad Network Corporation"
the output should be
"BroadNetworkCorporation"
Just iterate over the whole string in O(N) time removing any occurrence of ' '. There is no shorter (more efficient) way of doing this. The program is (read through the code and comments):
package main
import (
"fmt"
)
func removeSpaces(stri *[]rune) {
var N = len(*stri); //length of string
var spaceCounter = 0; //number of space characters
for i := 0; i < N; i++ {
if ((*stri)[i] == ' ') {
spaceCounter += 1;
continue;
} else {
(*stri)[i - spaceCounter] = (*stri)[i];
}
}
// Cut off the leftover characters at the end
var maxLen = N - spaceCounter
var str = (*stri)[:maxLen]
fmt.Printf("%c\n", str);
}
func main() {
var stri []rune = []rune("Broad Network Corporation"); //given string
removeSpaces(&stri); //note use of pointer scheme
}
The output is:
"B r o a d N e t w o r k C o r p o r a t i o n"
as expected. The time complexity is actually O(2N), with O(N) for finding the length of the input string array and O(N) for iterating over the string array in the called function. However, the coefficient (multiplier of 2 or 3 or 1/2, etc.) is normally omitted. The space complexity is O(N), since the input string array and the string array in the called function are the same array.
B) Removing all White-space Characters
Whitespace characters are as follows:
- '\n' (Newline): Moves the cursor to the beginning of the next line.
- '\t' (Horizontal Tab): Shifts the cursor to the next horizontal tab stop, typically moving it several spaces to the right.
- '\v' (Vertical Tab): Moves the cursor to the next vertical tab stop.
- '\r' (Carriage Return): Returns the cursor to the beginning of the current line without advancing to a new one.
- '\f' (Form Feed): Moves the cursor to the start of the next logical page, historically used for page breaks in printing.
- '\x20' (Space): While not typically quoted with a backslash (unless as \x20 in hexadecimal), the standard space character (pressing keyboard space-bar) is considered whitespace.
For an input of
"\tBroad Network Corporation\n"
the output should be
"BroadNetworkCorporation"
To remove any whitespace in a string, just follow the procedure in the above program, but check for the different alternate whitespace characters. The following program illustrates this (read through the code and comments):
package main
import (
"fmt"
)
func removeWhiteSpaces(stri *[]rune) {
var N = len(*stri); //length of string
var spaceCounter = 0; //number of space characters
for i := 0; i < N; i++ {
if ((*stri)[i] == '\u0020' || (*stri)[i] == '\n' || (*stri)[i] == '\r' || (*stri)[i] == '\t' || (*stri)[i] == '\u000B' || (*stri)[i] == '\f') { //\u0020 for ' ', \u000B for \v
spaceCounter += 1;
continue;
} else {
(*stri)[i - spaceCounter] = (*stri)[i];
}
}
// Cut off the leftover characters at the end
var maxLen = N - spaceCounter
var str = (*stri)[:maxLen]
fmt.Printf("%c\n", str);
}
func main() {
var stri []rune = []rune("\tBroad Network Corporation\n"); //given string
removeWhiteSpaces(&stri); //note use of pointer scheme
}
The output is:
"B r o a d N e t w o r k C o r p o r a t i o n"
as expected. The time complexity is officially O(N) and the space complexity is officially O(N)
Thanks for reading.
Related Links
More Related LinksCousins
BACK NEXTComments
Note: You can use the Search Box above to find articles and discussions of interest.