Broad Network


4.5 Remove whitespaces from a given string in C

4. Basic String Algorithm Problems in C

Full Course on Data Structures and Algorithms in C

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

#include 

void removeSpaces(char str[], int N) {
    int spaceCounter = 0;    //number of space characters
    int i;
    for (i = 0; i < N; i++) {
        if (str[i] == ' ') {
            spaceCounter += 1;
            continue;   
        } 
        else {
            str[i - spaceCounter] = str[i];
        }
    }
    
    //end string just after the new shortened length
    str[N-spaceCounter] = '\0';
}

int main(){
    int N = 0;    //length (size) of string to be determined in function below
    char stri[] = "Broad Network Corporation";    //given string
    
    char *ptr = stri;
    while (*ptr != '\0') {    //in C a string ends with '\0', which should not be counted
        ptr++; 
        N += 1;
    }

    removeSpaces(stri, N);
    
    // Print toggled characters
    int i=0;
    while (stri[i] != '\0') {
        printf("%c ", stri[i]);
        i++;
    }
    printf("\n");

    return 0;
}

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

#include 

void removeWhiteSpaces(char str[], int N) {
    int spaceCounter = 0;    //number of space characters
    int i;
    for (i = 0; i < N; i++) {
        if (str[i] == '\x20' || str[i] == '\n' || str[i] == '\r' || str[i] == '\t' || str[i] == '\v' || str[i] == '\f') {
            spaceCounter += 1;
            continue;   
        } 
        else {
            str[i - spaceCounter] = str[i];
        }
    }
    
    //end string just after the new shortened length
    str[N-spaceCounter] = '\0';
}

int main(){
    int N = 0;    //length (size) of string to be determined in function below
    char stri[] = "\tBroad Network Corporation\n";    //given string
    
    char *ptr = stri;
    while (*ptr != '\0') {    //in C a string ends with '\0', which should not be counted
        ptr++; 
        N += 1;
    }

    removeWhiteSpaces(stri, N);
    
    // Print toggled characters
    int i=0;
    while (stri[i] != '\0') {
        printf("%c ", stri[i]);
        i++;
    }
    printf("\n");

    return 0;
}

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 Links

Cousins

BACK NEXT

Comments


Note: You can use the Search Box above to find articles and discussions of interest.