19. Getting More from Your Strings
In This Chapter
- Employing character-testing functions
- Checking whether the case is correct
- Adding case-changing functions
- Using string functions
This chapter shows you ways to take work off your shoulders and put it on C’s. C includes many
helpful built-in functions in addition to ones such as strlen(), getchar(), and printf() that
you’ve read about so far.
Many more built-in functions exist than there is room for in a single chapter. This chapter explains the
most common and helpful character and string functions. In the next chapter, you’ll learn about some
numeric functions.
Character-Testing Functions
C has several built-in character-testing functions. Now that you know how to use getchar() and
getch() to get single characters, the character-testing functions can help you determine exactly
what kind of input characters your program receives. You can set up if logic to execute certain
courses of action based on the results of the character tests.
Tip
You must include the ctype.h header file at the top of any program that uses the
character functions described in this chapter.
The isalpha() function returns true (which is 1 to C) if the value in its parentheses is an
alphabetic character a through z (or the uppercase A through Z) and returns false (which is 0 to C)
if the value in parentheses is any other character. Consider this if:
Click here to view code image
if (isalpha(inChar))
{
printf("Your input was a letter.\n");
}
The message prints only if inChar contains an alphabetic letter.
C has a corresponding function named isdigit() that returns true if the character in the
parentheses is a number from 0 through 9. The following if prints A number if inChar contains
a digit: