Answer: C library provides two groups of functions which operate on characters and these are prototyped in header
1. Character classification functions
2. Character transformation functions
Character classification functions, for example,
int islower(int); int isupper(int); int iscntrl(int); int isdigit(int); int isxdigit(int); int isspace(int); int ispunct(int); int isalpha(int); int isalnum(int); int isgraph(int); int isprint(int);
classify the argument which is an integer containing character value. And function returns an integer, 0 or non-zero value for false and true respectively. Since standard doesn’t give a specified value, any non-zero value is considered true. For example,
char ch1 = 'A', ch2 = '7'; isupper(ch1); /* checks if 'ch1' is upper case character */ isdigit(ch2); /* tests if 'ch2' is a decimal digit */
Notice here that isupper(ch1) returns true if ‘ch1’ is upper case character else returns false. Similarly, isdigit(ch2) returns true if ‘ch2’ is a decimal digit else returns false. This way each classification function works.
Let’s, now, consider character transformation functions, for example,
int toupper(int); int tolower(int);
Transformation functions translate upper case characters to lower case and vice versa. toupper() returns upper case equivalent of its argument and tolower() returns lower case equivalent of its argument. If argument to either function is not a character of appropriate case, then it’s returned unchanged.
For example, If in some situation, you need to test a given character, say ‘ch’, is an upper case alphabet, then write test case as,
char ch = 'g'; if (isupper(ch)) ----;
rather than
if (ch >= 'A' && ch <= 'Z') ----;
First test case is portable with any ‘Character Set’ so it executes correctly.
Let’s consider an interesting program below,
/* char_operations.c -- program manipulates characters */ #include <stdio.h> #include <ctype.h> int main(int argc, char *argv[]) { int i; char *str; puts("\n**Program transforms character strings on cmd-line**\n"); for (i = 0; i < argc; i++) { str = argv[i]; if (i % 2 == 0) { /* convert string into upper case */ while (*str) putchar(toupper(*str++)); puts(""); } else if (i % 2 == 1) { /* convert string into lower case */ while (*str) putchar(tolower(*str++)); puts("") } } return 0; }
Output is as below:
[[email protected] ch09_StringsCharactersAndBytes]# gcc -o char_operations char_operations.c [[email protected] ch09_StringsCharactersAndBytes]# ./char_operations "converting lower case characters to upper case" "and upper case characters to lower case alternatively" "thank you" **Program transforms character strings on cmd-line** ./CHAR_OPERATIONS converting lower case characters to upper case AND UPPER CASE CHARACTERS TO LOWER CASE ALTERNATIVELY thank you
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Buy C Books
- Apply for C Internship
- Buy Computer Science Books