I loaded this branching demo sketch from a C++ book I've got and made some minor alterations to run it on an UNO. Entering from the serial monitor it correctly counts letters, digits, and punctuation but will not count spaces. For instance, entering "[color=blue]11 !! aa ,,[/color]"
displays 2 letters, 0 whitespace, 2 digits, 4 punctuation, 0 other.
The two #includes didn't make any difference so they're commented out. It also doesn't make any difference whether the function names have capital letters embedded - isdigit() works identically to isDigit().
I also tried a cout statement in the isspace() section and it never printed. So the space character never passes the 'is it a space' test.
I'm using #include <ArduinoSTL.h> to reduce the amount of modification needed to run the demo programs on the Arduino - mainly eliminating the getting-and-displaying-data translation tedium.
// name cctypes.cpp --
// C++ primer page 271
//#include <PrintStream.h>
#include <ArduinoSTL.h>
//#include <ctype.h> // prototypes for character functions
char ch;
int whitespace = 0;
int digits = 0;
int chars = 0;
int punct = 0;
int others = 0;
void setup() {
Serial.begin(115200);
}
void loop() {
std::cout << "Enter text for analysis, and type @"
" to terminate input.\n";
std::cin >> ch; // get first character
while (ch != '@') // test for sentinel
{
if (isAlpha(ch)) // is it an alphabetic character?
chars++;
else if (isspace(ch)) // is it a whitespace character?
whitespace++; // space counter doesn't work!?
else if (isdigit(ch)) // is it a digit?
digits++;
else if (ispunct(ch)) // is it punctuation?
punct++;
else
others++;
std::cin >> ch; // get next character
}
std::cout << chars << " letters, \n"
<< whitespace << " whitespace, \n"
<< digits << " digits, \n"
<< punct << " punctuations, \n"
<< others << " others.\n";
return 0;
}
Why is isspace()
not working? Possibly a quirk of the serial monitor? Or the library?