/*
* Some simple ASCII functions to check which category a char falls into
* Whether it be a letter (upper or lower case) a number or a symbol
* author: Danny Battison
* contact: gabehabe@hotmail.com
*/
#include <iostream> // used in the example only
// some preprocessor if statements to check if
// true and false have already been defined
// (if not, we define them)
#ifndef true
#define true 1
#endif
#ifndef false
#define false 0
#endif
bool isLower (char ch)
{ // check if a character is lower case (a-z)
if (ch >= 'a' && ch <= 'z')
return true;
else return false;
}
bool isUpper (char ch)
{ // check if a character is upper case (A-Z)
if (ch >= 'A' && ch <= 'Z')
return true;
else return false;
}
bool isLetter (char ch)
{ // check if a character a character (A-Z || a-z)
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= '\z'))
return true;
else return false;
}
bool isDigit (char ch)
{ // check if a character is a number (0-9)
if (ch >= '0' && ch <= '9')
return true;
else return false;
}
bool isSymbol (char ch)
{ // this one is more broken up.
// if you look at the ascii table, you will see that symbols are scattered
// into several groups, so we perform checks to see if it is between
// the values of said groups
if ((ch >= '!' && ch <= '/') || (ch >= ':' && ch <= '@')
||(ch >= '[' && ch <= '`') || (ch >= '{' && ch <= '~'))
return true;
else return false;
}
/** EXAMPLE USAGE **/
int main ()
{
char A = 'A';
char a = 'a';
char n = '4';
char s = ';';
// remember, by outputting a bool, we will get 0 or 1
// 0 is false, and 1 is true
std::cout << isLetter(A) << isUpper(A) << isLower(A) << isDigit(A) << isSymbol(A) << std::endl
<< isLetter(a) << isUpper(a) << isLower(a) << isDigit(a) << isSymbol(a) << std::endl
<< isLetter(n) << isUpper(n) << isLower(n) << isDigit(n) << isSymbol(n) << std::endl
<< isLetter(s) << isUpper(s) << isLower(s) << isDigit(s) << isSymbol(s) << std::endl;
std::cin.get ();
return EXIT_SUCCESS;
}