Friday, August 28, 2009

Functions to deal with character

A lot of useful functions for dealing with characters can be found in the library cctype:

 Funstion  Funcionality 
 isalpha(c);  Returns true if c is a letter (a-z, A-Z) 
 isupper(c);  Returns true if c is an upper-case letter 
 islower(c);  Returns true if c is a lower-case letter 
 isdigit(c);  Returns trud if c is a number(0-9) 
 isspace(c);  Returns true if c is a white space character 
 ispunct(c);  Returns true if c is a punctuation mark 
 toupper(c);  If c is a lower-case letter a upper-case letter is returned, otherwise c is returned 
 tolower(c);  If c is a upper-case letter a lower-case letter is returned, otherwise c is returned 


These can be used by adding #include <cctype> to the includes.

However, the return type and the type of the parameter of these functions is int. This can cause problems in some systems. A safe way to use the functions is always explicitly cast the parameter to int with static_cast<int>( c ); The same can be done to the return type of the toupper and tolower to make sure the character value is correct.


Previous post

Next post




No comments:

Post a Comment