Character Processing
Character Processing (<ctype.h>)
The <ctype.h> header file declares several functions for testing and converting characters.
-
For each function, the argument is an int whose value must be EOF or representable as an unsigned char.
-
The return value is an integer (non-zero = true, 0 = false).
Character Testing Functions
1. Alphanumeric Check
Returns a nonzero integer if c is an alphanumeric ASCII character.
Otherwise returns 0.
2. Alphabet Check
Returns nonzero if c is an alphabetic ASCII character, else 0.
3. Control Character Check
Returns nonzero if c is:
-
ASCII DEL character (177 octal / 0x7F hex), OR
-
Any non-printing ASCII character (< 40 octal / 0x20 hex)
Else returns 0.
4. Digit Check
Returns nonzero if c is a decimal digit (0–9), else 0.
5. Graphic Character Check
Returns nonzero if c is a printing character except space, else 0.
6. Lowercase Check
Returns nonzero if c is a lowercase alphabet, else 0.
7. Printable Character Check
Returns nonzero if c is a printable ASCII character including space, else 0.
8. Punctuation Check
Returns nonzero if c is an ASCII punctuation character
(any printable, non-alphanumeric character > 40 octal / 0x20 hex).
9. White Space Check
Returns nonzero if c is a whitespace character.
Standard whitespace characters:
-
Space
' ' -
Form feed
'\f' -
New line
'\n' -
Carriage return
'\r' -
Horizontal tab
'\t' -
Vertical tab
'\v'
10. Uppercase Check
Returns nonzero if c is an uppercase alphabet, else 0.
11. Hexadecimal Digit Check
Returns nonzero if c is a hex digit (0–9, A–F, a–f).
Character Conversion Functions
Convert to Lowercase
Converts uppercase letter to lowercase.
If not uppercase, value remains unchanged.
Convert to Uppercase
Converts lowercase letter to uppercase.
If not lowercase, value remains unchanged.
Example 1: Count Alphabets, Digits, and Spaces
Output
Example 2: Check Uppercase or Lowercase Alphabet
Example 3: Count Uppercase and Lowercase Characters (University Question)
Note
We can also use built-in <ctype.h> functions such as:
-
isupper() -
islower()
to simplify such programs.
Comments
Post a Comment