Basics of Character Manipulation Functions for C Programming

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",phrase);
 puts("Statistics:");
 printf("%d alphabetic characters\n",alpha);
 printf("%d blanks\n",blank);
 printf("%d punctuation symbols\n",punct);
 return(0);
}

Text Statistics may seem long, but it’s not; the phrase[] string declared at Line 6 can be anything you like — any text, a poem, or a filthy limerick. It should be long enough to have a smattering of interesting characters. Note that although the text wraps and indents in this text, you should just type one long line of text in your code.

This code also does a gang initialization:

 alpha = blank = punct = 0;

Because each of those variables must be set to 0, you use multiple assignment operators on the same line and accomplish the task in one fell swoop.

The meat of the program’s operation takes place starting with the gather data comment. A while loop steps through each character in the string. The condition for the while loop is phrase[index]. That evaluation is true for each character in the array except for the last one, the null character, which evaluates to FALSE and stops the loop.

CTYPE functions are used in if statements as each character is evaluated at Lines 17, 19, and 21. In if-else tests, every character must be checked. When a positive or TRUE match is found, a counter variable is incremented.

Exercise 1: Type the source code from Text Statistics into your editor. Build and run.

Exercise 2: Modify the source code from Text Statistics so that tests are also made for counting upper- and lowercase letters. Display those results as well.

Exercise 3: Add code to your solution to Exercise 2 so that a final tally of all characters in the text (the text’s length) is displayed as the final statistic.

How to change characters

The CTYPE functions that begin with to are used to convert characters. The most common of these functions are toupper() and tolower(), which come in handy when testing input. As an example, consider the typical yorn problem, illustrated in A Yorn Problem.

A YORN PROBLEM

#include <stdio.h>
#include <ctype.h>
int main()
{
 char answer;
 printf("Would you like to blow up the moon? ");
 scanf("%c",&answer);
 answer = toupper(answer);
 if(answer=='Y')
 puts("BOOM!");
 else
 puts("The moon is safe");
 return(0);
}

Yorn is programmer-speak for a yes-or-no situation: The user is asked to type Y for Yes or N for No. Does the person have to type Y or y? Or can they type N or n, or would any non-Y key be considered No?

In A Yorn Problem, Line 10 uses toupper() to convert the character input to uppercase. That way, only a single if condition is required to test for Y or y input.

Exercise 4: Create a new project using the source code shown in A Yorn Problem. Build and run.

Exercise 5: Modify the source code so that text is displayed when the user types neither Y nor N.

Exercise 6: Write a program that changes all the uppercase letters in a string of text to lowercase and changes the lowercase letters to uppercase. Display the results.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com