(Write Your Own Character Handling Functions) Using the ASCII character chart in Appendix B as a guide, write your own versions of the character-handling functions in Fig. 22.17.

What will be an ideal response?


```
#include
using namespace std;

// prototypes
int isDigit( int );
int isAlpha( int );
int isAlNum( int );
int isLower( int );
int isUpper( int );
int isSpace( int );
int isPunct( int );
int isPrint( int );
int isGraph( int );
int toLower( int );
int toUpper( int );

int main()
{
int v;
char a; // hold a character
char header[] = "According to";
char *names[] = { "isDigit ", "isAlpha ", "isAlNum ", "isLower ",
"isUpper ", "isSpace ", "isPunct ", "isPrint ", "isGraph ",
"toLower ", "toUpper " };
char *names2[] = { "digit", "letter", "letter/digit", "lowercase",
"uppercase", "space", "punctuation", "print", "graph",
"converted lowercase", "converted uppercase" };
int ( *f[] )( int ) = { isDigit, isAlpha, isAlNum, isLower,
isUpper, isSpace, isPunct, isPrint,
isGraph, toLower, toUpper };

cout << "Enter a character: ";
cin >> a;

for ( int k = 0; k < 11; ++k )
{
v = ( *f[ k ] )( static_cast< int >( a ) );
cout.write( header, 13 );
cout << names[ k ] << a << ( !v ? " is not a " : " is a " )
<< names2[ k ] << " character\n";
} // end for

return 0;
} // end main

// Normally these would return bool, but int return type
// is consistent with the ctype library.

// determine whether c is an integer
int isDigit( int c )
{
return ( c >= 48 && c <= 57 ) ? 1 : 0;
} // end function isDigit

// return true if c is a letter
int isAlpha( int c )
{
return ( ( c >= 65 && c <= 90 ) || ( c >= 97 && c <= 122 ) ) ? 1 : 0;
} // end function isAlpha

// return true if c is a digit
int isAlNum( int c )
{
return ( isDigit( c ) == 1 || isAlpha( c ) == 1 ) ? 1 : 0;
} // end function isAlNum

// return true if c is lowercase letter
int isLower( int c )
{
return ( c >= 97 && c <= 122 ) ? 1 : 0;
} // end function isLower

// return true if c is uppercase letter
int isUpper( int c )
{
return ( c >= 65 && c <= 90 ) ? 1 : 0;
} // end function isUpper

// return true if c is whitespace character
int isSpace( int c )
{
return ( ( c == 32 ) || ( c >= 9 && c <= 13 ) ) ? 1 : 0;
} // end function isSpace

// return true if c is a printing character other than a space
int isPunct( int c )
{
return ( isAlNum( c ) == 0 && isSpace( c ) == 0 ) ? 1 : 0;
} // end function isPunct

// return true value if c is a printing variable including space
int isPrint( int c )
{
return ( c >= 32 && c <= 126 ) ? 1 : 0;
} // end function isPrint

// return true if c is a printing character other than space
int isGraph( int c )
{
return ( c >= 33 && c <= 126 ) ? 1 : 0;
} // end function isGraph

// convert to lowercase
int toLower( int c )
{
return ( isUpper( c ) == 1 ) ? c + 32 : c;
} // end function toLower

// convert to uppercase
int toUpper( int c )
{
return ( isLower( c ) == 1 ) ? c - 32 : c;
} // end function toUpper
```
Enter a character: A
According to isDigit, A is not a digit character
According to isAlpha, A is a letter character
According to isAlNum, A is a letter/digit character
According to isLower, A is not a lowercase character
According to isUpper, A is a uppercase character
According to isSpace, A is not a space character
According to isPunct, A is not a punctuation character
According to isPrint, A is a print character
According to isGraph, A is a graph character
According to toLower, A is a converted lowercase character
According to toUpper, A is a converted uppercase character
Enter a character: 4
According to isDigit, 4 is a digit character
According to isAlpha, 4 is not a letter character
According to isAlNum, 4 is a letter/digit character
According to isLower, 4 is not a lowercase character
According to isUpper, 4 is not a uppercase character
According to isSpace, 4 is not a space character
According to isPunct, 4 is not a punctuation character
According to isPrint, 4 is a print character
According to isGraph, 4 is a graph character
According to toLower, 4 is a converted lowercase character
According to toUpper, 4 is a converted uppercase character

Computer Science & Information Technology

You might also like to view...

The default font in a Word document is ________

A) Calibri B) Times New Roman C) Arial D) Comic Sans

Computer Science & Information Technology

Which of the following is considered a dual-outcome selection?

A. If Not balance < 200 Then    Display "Your account will continue to earn interest." End If B. If personHasMoney And showedID Then    ticketPrice = discountPrice Else    ticketPrice = regularPrice End If C. If personHasMoney And showedID Then    ticketPrice = discountPrice End If D. If password != "sugarBear" Then    Display "Sorry, incorrect password." End If

Computer Science & Information Technology

Excel is able to group data both by rows and by columns in a subtotal.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

In which phase of the software development life cycle are the necessary hardware and programming languages/tools acquired?

What will be an ideal response?

Computer Science & Information Technology