0

C Character Functions

There are several types of character functions in the standard C library. One type of character functions is character classification functions. They are isuppper, islower, isdigit, isspace, and other boolean functions. For example, if (isupper(ch)) determines whether ch is upper case or not.

Another type of character functions is character conversion functions. They are toupper, tolower, and others. For example, ch = toupper(ch) converts ch to upper case whether the original is lower case or upper case.

Here is the same code for the character functions:

/*
 *
 * ctype.c
 *
 * Program to show examples of the use of isupper, toupper, etc.
 *
 * by Mark Virtue, 2001.
 *
 */

#include <stdio.h>
#include <ctype.h>

main() {
 char ch;

 printf("Please type in a character (enter q to quite): ");
 scanf("%c", &ch);

 while (ch != 'q') {
  if (isupper(ch)) {
   printf("This is an uppercase charactern");
   ch = tolower(ch);
   printf("Lowercase equivalent is '%c'n", ch);
  }
  else if (islower(ch))
   printf("This is an lowercase charactern");
  else if (isdigit(ch))
   printf("This is an digitn");
  else if (isspace(ch))
   printf("This is an space-type charn");
  else
   printf("This is an unknown charn");

  printf("Please type a character (enter q to quite): ");
  while (getchar() != 'n' && getchar() != EOF) {}
  scanf("%c", &ch);
 }

 fflush(stdin);
}