08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7 ■ Functions

int isLowerCase(char ch) {

return ch >= 'a' && ch <= 'z';

} //end isLowerCase

int position(char ch) {

int isUpperCase(char), isLowerCase(char);

if (isUpperCase(ch)) return ch - 'A' + 1;

if (isLowerCase(ch)) return ch - 'a' + 1;

return 0;

} //end isPosition

Here is a sample run of P7.10:

Type some letters and non-letters and press "Enter"

FaT($hY&n

F 6

a 1

T 20

( 0

$ 0

h 8

Y 25

& 0

n 14

We have written the functions isDigit, isUpperCase, isLowerCase, and isLetter to

illustrate basic concepts about character functions. However, C provides a number of predefined

functions (actually, macros, but the distinction is not important for us) for working with

characters. Among these are isdigit (test for a digit), isupper (test for an uppercase letter),

islower (test for a lowercase letter), and isalpha (test for a letter). To use these functions, you

need to place the directive

#include <ctype.h>

at the head of your program. As an exercise, rewrite P7.10 using isupper and islower. Without

isUpperCase, isLowerCase and their prototypes, your program would be much shorter.

190

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!