forked from TheRenegadeCoder/sample-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Remove All Whitespace in C (TheRenegadeCoder#3977)
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |