Basics of Character Manipulation Functions for C Programming

By Dan Gookin

At the heart of any string of text is the char variable. With C programming, it’s a unique cubby hole, into which you stuff a value from 0 through 255. That value is represented visually as a character.

Basics of the CTYPEs

The C language features a bevy of functions designed to test or manipulate individual characters. The functions are all defined in the ctype.h header file. Most programmers therefore refer to the functions as the CTYPE functions, where CTYPE is pronounced “see-type,” and not “stoor-ye,” which how a native Russian would read it.

To use the CTYPE functions, the ctype.h header file must be included in your source code:

#include <ctype.h>

CTYPE functions fit into two categories: testing and manipulation.

Function Returns TRUE When ch is
isalnum(ch) A letter of the alphabet (upper- or lowercase) or a number
isalpha(ch) An upper- or lowercase letter of the alphabet
isascii(ch) An ASCII value in the range of 0 through 127
isblank(ch) A tab or space or another blank character
iscntrl(ch) A control code character, values 0 through 31 and 127
isdigit(ch) A character 0 through 9
isgraph(ch) Any printable character except for the space
ishexnumber(ch) Any hexadecimal digit, 0 through 9 or A through F (upper- or
lowercase)
islower(ch) A lowercase letter of the alphabet, a to z
isnumber(ch) See isdigit()
isprint(ch) Any character that can be displayed, including the space
ispunct(ch) A punctuation symbol
isspace(ch) A white-space character, space, tab, form feed, or an Enter,
for example
isupper(ch) An uppercase letter of the alphabet, A to Z
isxdigit(ch) See ishexnumber()
Function Returns
toascii(ch) The ASCII code value of ch, in the range of 0 through 127
tolower(ch) The lowercase of character ch
toupper(ch) The uppercase of character ch

Generally speaking, testing functions begin with is, and conversion functions begin with to.

Every CTYPE function accepts an int value as the argument, represented by the variable ch in Tables 13-1 and 13-2. These are not char functions!

Every CTYPE function returns an int value. For the functions that return logical TRUE or FALSE values, FALSE is 0, and TRUE is a non-zero value.

Howt to test characters

The CTYPE functions come in most handy when testing input, determining that the proper information was typed, or pulling required information out of junk. The code in Text Statistics illustrates how a program can scan text, pluck out certain attributes, and then display a summary of that information.

TEXT STATISTICS

#include <stdio.h>
#include <ctype.h>
int main()
{
char phrase[] = "When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.";
int index,alpha,blank,punct;
alpha = blank = punct = 0;
/* gather data */
index = 0;
while(phrase[index])
{
if(isalpha(phrase[index]))
alpha++;
if(isblank(phrase[index]))
blank++;
if(ispunct(phrase[index]))
punct++;
index++;
}
/* print results */
printf("%s"n"