char classification

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 11 Exercise 5
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PPP
//	Chapter 11 Exercise 5

#include "std_lib_facilities.h"

void calculate() {
	while (cin) {
		string word;
		char quit;
		cout << "enter string or char to decompose and classifie" << endl;
		if (cin >> quit && quit == 'x') {
			break;
		}
		cin.putback(quit);
		if (cin >> word) {
			for (char c : word) {
				if (isspace(c)) {
					cout << c << " is a space." << endl;
				}
				if (isalpha(c)) {
					cout << c << " is a letter." << endl;
				}
				if (isdigit(c)) {
					cout << c << " is a decimal digit." << endl;
				}
				if (isxdigit(c)) {
					cout << c << " is a hexadecimal digit." << endl;
				}
				if (isupper(c)) {
					cout << c << " is an upper case letter." << endl;
				}
				if (islower(c)) {
					cout << c << " is an lower case letter." << endl;
				}
				if (isalnum(c)) {
					cout << c << " is a letter or a decimal digit." << endl;
				}
				if (iscntrl(c)) {
					cout << c << " is a control character." << endl;
				}
				if (ispunct(c)) {
					cout << c << " is not a letter, digit, whitespace or invisible control character." << endl;
				}
				if (isprint(c)) {
					cout << c << " is printable." << endl;
				}
				if (isgraph(c)) {
					cout << c << " is isalpha(c) or isdigit(c) or ispunct(c)." << endl;
				}
			}
		}
		else {
			cin.clear();
			cout << "enter a valid string or x to quit" << endl;
		}
	}
}

int main()
try
{
	calculate();
	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
enter string or char to decompose and classifie
hello
h is a letter.
h is an lower case letter.
h is a letter or a decimal digit.
h is printable.
h is isalpha(c) or isdigit(c) or ispunct(c).
e is a letter.
e is a hexadecimal digit.
e is an lower case letter.
e is a letter or a decimal digit.
e is printable.
e is isalpha(c) or isdigit(c) or ispunct(c).
l is a letter.
l is an lower case letter.
l is a letter or a decimal digit.
l is printable.
l is isalpha(c) or isdigit(c) or ispunct(c).
l is a letter.
l is an lower case letter.
l is a letter or a decimal digit.
l is printable.
l is isalpha(c) or isdigit(c) or ispunct(c).
o is a letter.
o is an lower case letter.
o is a letter or a decimal digit.
o is printable.
o is isalpha(c) or isdigit(c) or ispunct(c).
enter string or char to decompose and classifie

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.