Last active
October 2, 2019 14:52
-
-
Save airween/c2f3ff400e4c4a94dcbed9aec6eca48a to your computer and use it in GitHub Desktop.
removeWhitespace - test the char casting
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
/* | |
clang++ -Wall rmwsp.cc | |
g++ -Wall rmwsp.cc | |
*/ | |
#include <string> | |
#include <iostream> | |
#define NBSP 160 | |
typedef std::basic_string <unsigned char> ustring; | |
int main(int argc, char** argv) { | |
std::string value; | |
std::cout << "Casted constant: " << +static_cast<char>(NBSP) << std::endl; | |
if (argc > 1) { | |
std::cout << "Got argument..." << std::endl; | |
value = std::string(argv[1]); | |
} | |
else { | |
std::cout << "No argument, using default..." << std::endl; | |
unsigned char cvalue[] = {102, 111, 111, 160, 98, 97, 114}; | |
std::string uvalue(cvalue, cvalue+7); | |
value = uvalue; | |
} | |
std::cout << "Old string: '" << value << "'" << std::endl; | |
int64_t i = 0; | |
while (i < value.size()) { | |
// see the numeric values of char items | |
std::cout << +value[i] << std::endl; | |
// remove whitespaces and non breaking spaces (NBSP) | |
if (isspace(value[i]) || (value[i] == static_cast<char>(NBSP))) { | |
value.erase(i, 1); | |
} else { | |
/* if the space is not a whitespace char, increment counter | |
counter should not be incremented if a character is erased because | |
the index erased will be replaced by the following character */ | |
i++; | |
} | |
} | |
std::cout << "New string: '" << value << "'" << std::endl; | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment