C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
if (isdigit(inChar))
{
printf("A number\n");
}

Note

Do you see why these are called character-testing functions? Both isalpha() and
isdigit() test character content and return the relational result of the test.

Is the Case Correct?


The isupper() and islower() functions let you know whether a variable contains an upper- or
lowercase value. Using isupper() keeps you from having to write long if statements like this:


Click here to view code image


if ((inLetter >= 'A') && (inLetter <= 'Z'))
{
printf("Letter is uppercase\n");
}

Instead, use isupper() in place of the logical comparison:


Click here to view code image


if (isupper(inLetter))
{
printf("Letter is uppercase\n");
}

Note

islower() tests for lowercase values in the same way as isupper() tests for
uppercase values.

You might want to use isupper() to ensure that your user enters an initial-uppercase letter when
entering names.


Here’s a quick little program that gets a username and password and then uses the functions described
in this chapter to check whether the password has an uppercase letter, a lowercase letter, and a
number in it. If a user has all three, the program congratulates him or her for selecting a password
with enough variety to make it harder to crack. If the password entered does not have all three
categories, the program suggests that the user consider a stronger password.


Click here to view code image


// Example program #1 from Chapter 19 of Absolute Beginner's Guide
// to C, 3rd Edition
Free download pdf