Check character group as alpha, digit, control, punctuation, lower, upper etc.
Check for character grouping in the ascii 7 bit range of control character, digit, punctuation, alphabet, alphanumeric, lowercase, uppercase, printable, graphical, white space etc.
Learning Objectives
- To check the single character that it belongs to which group of character amongst the lower case alphabet, upper case alphabet, alphabet, alphanumeric, printable, graphical, punctuation, control character or white-space character.
Source Code
|
|
Run Output
Code Understanding
int alpha=0, digit=0, alnum=0, lower=0, upper=0, print=0, graph=0, cntrl=0, punct=0, space=0;
Here all the variables have been initialised with 0 so that later the count of related information can be filled into it.
for(int i=0;i<=127;++i)
This loop traverses the entire range of 7-bit ASCII code which is from ascii decimal code value 0 to 127
if(islower(i)) ++lower;
islower() checks if the given code range belongs to ascii range 97 to 122. 26 alphabets are there in the lower range.
if(isupper(i)) ++upper;
isupper() checks if the given code range belongs to ascii range 65 to 90. 26 alphabets are there in the upper range as well.
if(isalpha(i)) ++alpha;
isalpha() checks if the code belongs to an alphabet whether lowercase or uppoercase, so this figure should be 52 in the given range.
if(isdigit(i)) ++digit;
isdigit() checks for a digit which are 0 to 9 and ascii code – 48 to 57. So in the given range of 0-127 hey will be 10.
if(isalnum(i)) ++alnum;
isalnum() checks for presence in the range of alphabets or in numbers. So in the given range it will be 52+10 means 62.
if(isprint(i)) ++print;
isprint() checks if the character is printable. character from 32 decimal to 126 decimal ASCII are printable. Space character at ASCII value 32 is considered printable. This total comes 95.
if(isgraph(i)) ++graph;
isgraph() finds characters that have a printable graphical representation. For this space is dropped from the graphical representation and it is from ASCII 33 to 126. Hence this will return 94 in the given range.
if(ispunct(i)) ++punct;
Punctuation marks are from ASCII code ranges33-47, 58-64, 91-96,123-126. This total will come 32.
if(iscntrl(i)) ++cntrl;
iscntrl() gives the check for control character which are from ASCII 0 to ASCII 31 and ASCII 127. This gives 33.
if(isspace(i)) ++space;
Space character means the so called whitespace providing characters which are space, horizontal tab, vertical tab, new line, carriage return and form feed characters. So this adds up to 6.
cout<<“Lowercase = “<<lower<<endl; cout<<“Uppercase = “<<upper<<endl;
cout<<“Alphabets = “<<alpha<<endl; cout<<“Digits = “<<digit<<endl;
cout<<“Alpha-Numerics = “<<alnum<<endl; cout<<“Printable = “<<print<<endl;
cout<<“Graphical = “<<graph<<endl; cout<<“Punctuations = “<<punct<<endl;
cout<<“Control Chars = “<<cntrl<<endl; cout<<“Whitespace Chars = “<<space<<endl;
The given counts in the range 0 to 127 are printed with the above statements.
Notes
- Other than these checks, the check for isblank() has also been added in c++11 standard. This checks for space and horizontal tab character.
- In modern compilers the use of cctype header is recommended in place of ctype.h
Suggested Filename(s): char-group.cpp
Created: 3-Jan-2018 22:32 | Updated: 4-Jan-2018 00:43|
