1. Home
  2. Computing & Technology
  3. C / C++ / C#

C Tutorial - Strings and Text Handling

By David Bolton, About.com

5 of 9

The Char Functions in ctype.h

Using ctype.h

The ctype.h library has functions for classifying characters. Each of the functions below returns an int when passed in a char.
  • isalnum() Check if character is alphanumeric (isalpha) | isdigit()
  • isalpha() - Check if character is alphabetic
  • iscntrl() - Check if character is a control character
  • isdigit() - Check if character is decimal digit
  • isgraph() - Check if character has graphical representation
  • islower() - Check if character is lowercase letter
  • isprint() - Check if character is printable
  • ispunct() - Check if character is a punctuation character
  • isspace() - Check if character is a white-space
  • isupper() - Check if character is uppercase letter
  • isxdigit() - Check if character is hexadecimal digit
These values returned from theese functions aren't just 0 or 1 type. 0 is returned for false but isalpha() for example returns 1 if the char is upper case and 2 if its lower case. Some of the values returned depend upon your locale so you'll get different results in different countries.

There are a couple of ancillary functions for converting upper case letter to lower case etc.

  • tolower() - Converts upper case letter to lower case
  • toupper() - Converts lower case letter to upper case
If you are doing any kind of input validation then the functions in ctype.h will simplify things for you.

Download Example Three

This example calls most of the functions except upper and lower case for each value in the range 0 to 127. You can see for example how isalnum() is just isalpha() binary ored with isdigit().

On the next page : Using the str functions

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C
  5. C Tutorials
  6. The Char Functions in ctype.h

©2009 About.com, a part of The New York Times Company.

All rights reserved.