C Language Library Functions Interview Questions and Answers For Freshers Part-4
The header file ctype.h defines various functions for determining what class a character belongs to. These consist of the following functions:
Function Character Class Returns Nonzero for Characters
isdigit() – Decimal digits – 0-9
isxdigit() – Hexadecimal digits – 0-9, a-f, or A-F
isalnum() – Alphanumerics – 0-9, a-z, or A-Z
isalpha() – Alphabetics – a-z or A-Z
islower() – Lowercase alphabetics – a-z
isupper() – Uppercase alphabetics – A-Z
isspace() – Whitespace – Space, tab, vertical tab, newline, form feed, or carriage return
isgraph() – Nonblank characters – Any character that appears nonblank when printed (ASCII 0x21 through 0x7E)
isprint() – Printable characters – All the isgraph() characters, plus space
ispunct() – Punctuation – Any character in isgraph() that is not in isalnum()
iscntrl() – Control characters – Any character not in isprint() (ASCII 0x00 through 0x1F plus 0x7F)
There are three very good reasons for calling these macros instead of writing your own tests for character classes. They are pretty much the same reasons for using standard library functions in the first place. First, these macros are fast. Because they are generally implemented as a table lookup with some bit-masking magic, even a relatively complicated test can be performed much faster than an actual comparison of the value of the character.
Second, these macros are correct. It’s all too easy to make an error in logic or typing and include a wrong character (or exclude a right one) from a test.
The header file ctype.h also defines two functions to convert characters between upper- and lowercase alphabetics. These are toupper() and tolower(). The behavior of toupper() and tolower() is undefined if their arguments are not lower- and uppercase alphabetic characters, respectively, so you must remember to check using islower() and isupper() before calling toupper() and tolower().
Related articles across the web