The Standard C Library for Linux Issue 32

The last article was on Input and Output.  This article is on character handling.

Character handling allows us to clasify characters as alpha, digit, hexdigit, whitespace, printable, lowercase, uppercase, punctuation and to map to and from the upper and lowercase alphabets.  Most importantly implements these functions in a non-system dependent way.

If you write your program assuming that every computer is an ASCII computer you will have trouble porting your program to non ASCII machines. If you write your character handling functions in terms of these functions your program will be much more portable to other platforms.

I am assuming a knowledge of c programming on the part of the reader.  There is no guarantee of accuracy in any of this information nor suitability for any purpose.

The program example that I will do this month will go thru the entire 8bit ASCII range and tell us to which classes any one chacter belongs.  The example is rogers_example04.c. The output the program generates will be an html document and the run from my system is rogers_example04.html . This program can be used as a cgi-bin  script and is a demonstration of the flexibility of the c language.

As always, if you see an error in my documentation please tell me and I will correct myself in a later document.  See corrections at end of the document to review corrections to the previous articles.

Character Handling

 
#include 

int isalpha(int c);
int isalnum(int c);
int isdigit(int c);
int isxdigit(int c);

int iscntrl(int c);
int isspace(int c);

int ispunct(int c);
int isgraph(int c);
int isprint(int c);

int islower(int c);
int isupper(int c);

int tolower(int c);
int toupper(int c);

isalpha returns true if the character is in the range of A-Z or a-z.

READ  BogoMips mini-Howto: Why to pay attention to BogoMips

isalnum returns true if the character is in the range of A-Z or a-z or 0-9.

isdigit returns true if the character is in the range of 0-9.

isxdigit returns true if the character is in the range of 0-9 or a-f or A-F.

iscntrl returns true if the character is in the set (FF, NL, CR, HT, VT, BEL or BS).

isspace returns true if the character is in the set (space, FF, NL, CR, HT or VT).

ispunct returns true if the character is a nonalnum, nonspace and noncntrl.

isgraph returns true if the character isalnum or ispunct.

isprint returns true if the character isspace or isgraph.

islower returns true if the character is in the range of a-z.

isupper returns true if the character is in the range of A-Z.

tolower if isupper return the lowercase character otherwise return the character.

toupper if islower return the uppercase character otherwise return the character.
 

Bibilography:

The ANSI C Programming Language, Second Edition, Brian W. Kernighan, Dennis M. Ritchie, Printice Hall Software Series, 1988

The Standard C Library, P. J. Plauger, Printice Hall P T R, 1992

The Standard C Library, Parts 1, 2, and 3, Chuck Allison, C/C++ Users Journal, January, February, March 1995

CTYPE(3), BSD MANPAGE, Linux Programmer’s Manual, 29 November 1993

 

The Standard C Library for Linux, Part One, James M. Rogers, January 1998
The Standard C Library for Linux, Part Two, James M. Rogers, July 1998
The Standard C Library for Linux, Part Three, James M. Rogers, August 1998

indexnew-4431002 homenew-2226243 back2-7672948 fwd-7545835