Remove All Whitespace in C

Published on 06 November 2024 (Updated: 06 November 2024)

Welcome to the Remove All Whitespace in C page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char *argv[]) {
    // check whether the passed argument is a string or empty
    if (argc < 2 || argv[1][0] == '\0') {
        printf("Usage: please provide a string\n");
        return 1; 
    }

    char *input = argv[1]; 
    char output[1000];  
    int j = 0; 

    for (int i = 0; input[i] != '\0'; i++) {
        // check if current character is not a whitespace character
        if (!isspace(input[i])) {
            output[j++] = input[i];
        }
    }

    // null terminator to mark the end of a string
    output[j] = '\0';

    // print the output string with no spaces
    printf("%s\n", output);

    return 0;
}

Remove All Whitespace in C was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.