Skip to content

Commit

Permalink
Add Remove All Whitespace in C++ (TheRenegadeCoder#3985)
Browse files Browse the repository at this point in the history
  • Loading branch information
LauraV-702 authored Nov 7, 2024
1 parent 0c0a877 commit 16e0aa9
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions archive/c/c-plus-plus/remove-all-whitespace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <string>
#include <cctype> // for std::isspace


int main(int argc, char* argv[]) {

// Check if given string passed
if (argc < 2) {
std::cout << "Usage: please provide a string" << std::endl;
return 1; // Return error code if no string is given
}

// Get the string passed
std::string input = argv[1];
std::string result;

// Check if input string is empty
if (input.empty()) {
std::cout << "Usage: please provide a string" << std::endl;
return 1; // Exit error code if the string is empty
}

for(char c : input) {
// If the character is not a whitespace character, add it to result
if (!std::isspace(static_cast<unsigned char>(c))) {
result += c;
}
}
//print out the result (string should have no spaces)
std::cout << result << std::endl;

return 0;
}

0 comments on commit 16e0aa9

Please sign in to comment.