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#3985)
- Loading branch information
1 parent
0c0a877
commit 16e0aa9
Showing
1 changed file
with
34 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,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; | ||
} |