Skip to content

Commit

Permalink
Add Remove All Whitespace in C (TheRenegadeCoder#3977)
Browse files Browse the repository at this point in the history
  • Loading branch information
qopci authored Nov 6, 2024
1 parent 9512abe commit c622ece
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions archive/c/c/remove-all-whitespace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#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;
}

0 comments on commit c622ece

Please sign in to comment.