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

int isalnum(int c);

Returns a nonzero integer if c is an alphanumeric ASCII character.
Otherwise returns 0.


2. Alphabet Check

int isalpha(int c);

Returns nonzero if c is an alphabetic ASCII character, else 0.


3. Control Character Check

int iscntrl(int c);

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

int isdigit(int c);

Returns nonzero if c is a decimal digit (0–9), else 0.


5. Graphic Character Check

int isgraph(int c);

Returns nonzero if c is a printing character except space, else 0.


6. Lowercase Check

int islower(int c);

Returns nonzero if c is a lowercase alphabet, else 0.


7. Printable Character Check

int isprint(int c);

Returns nonzero if c is a printable ASCII character including space, else 0.


8. Punctuation Check

int ispunct(int c);

Returns nonzero if c is an ASCII punctuation character
(any printable, non-alphanumeric character > 40 octal / 0x20 hex).


9. White Space Check

int isspace(int c);

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

int isupper(int c);

Returns nonzero if c is an uppercase alphabet, else 0.


11. Hexadecimal Digit Check

int isxdigit(int c);

Returns nonzero if c is a hex digit (0–9, A–F, a–f).


Character Conversion Functions

Convert to Lowercase

int tolower(int c);

Converts uppercase letter to lowercase.
If not uppercase, value remains unchanged.


Convert to Uppercase

int toupper(int c);

Converts lowercase letter to uppercase.
If not lowercase, value remains unchanged.


Example 1: Count Alphabets, Digits, and Spaces

#include <stdio.h> #include <string.h> #include <ctype.h> int main(void) { char str[100]; int i, dc=0, al=0, sp=0; printf("Enter a string \n"); fgets(str,100,stdin); for(i=0;i<strlen(str);i++) { if(isalpha(str[i])) al++; if(isdigit(str[i])) dc++; if(isspace(str[i])) sp++; } printf("Number of digits=%d\n",dc); printf("Number of alphabets=%d\n",al); printf("Number of spaces=%d\n",sp); }

Output

Enter a string this is test123 Number of digits=3 Number of alphabets=10 Number of spaces=3

Example 2: Check Uppercase or Lowercase Alphabet

#include <stdio.h> #include <ctype.h> int main() { char ch; printf("Enter a character\n"); ch = getchar(); if(isupper(ch)) printf("uppercase\n"); else printf("lower case\n"); }

Example 3: Count Uppercase and Lowercase Characters (University Question)

#include <stdio.h> int main() { char str[100]; int uppercase_count = 0, lowercase_count = 0; int i = 0; printf("Enter a string: "); fgets(str, sizeof(str), stdin); while (str[i] != '\0') { if (str[i] >= 'A' && str[i] <= 'Z') { uppercase_count++; } else if (str[i] >= 'a' && str[i] <= 'z') { lowercase_count++; } i++; } printf("Number of uppercase characters: %d\n", uppercase_count); printf("Number of lowercase characters: %d\n", lowercase_count); return 0; }

Note

We can also use built-in <ctype.h> functions such as:

  • isupper()

  • islower()

to simplify such programs.

Comments

Popular posts from this blog

Programming in C GXEST204 - KTU 2024 scheme syllabus notes pdf ------- Dr Binu V P

Structure of a C Program

Single and Multi Dimensional Arrays