Answer to Question 2.5

/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */

*******************************************************************
//:2_5a_a1.c
for (int ch = 'a'; ch <= 'z'; ch++)
cout << chr(ch) << " " << ch << "\n";

//: "obvious" solution to print letters and digits
// version 1
#include <stream.h>
int main(int, char**)
{
#include "2_5a_a1.c"	/* EXPAND */
    for (ch = '0'; ch <= '9'; ch++)
        cout << chr(ch) << " " << ch << "\n";
    return 0;
}


//: print letters and digits
// version 2
// check each letter before printing
#include <stream.h>
#include <ctype.h>
void pr(int i)
{
    cout << chr(i) << " " << i << "\n";
}

int main(int, char**)
{
    for (int i = 'a'; i <= 'z'; i++)
	if (isalpha(i))
	    pr(i);

    for (i = '0'; i <= '9'; i++)
	pr(i);
    return 0;
}

//: print all letters and digits, taking into
// consideration non-English alphabets
// version 3
#include <limits.h>
#include <stream.h>
#include <ctype.h>

void pr(int i)	// same pr() as before
{
    cout << chr(i) << " " << i << "\n";
}

int main(int, char**)
{
    for (int i = 0; i <= CHAR_MAX; i++)
	if (islower(i) || isdigit(i))
	    pr(i);
 
    return 0;
}
*******************************************************************

//: print all printable characters
// version 1
#include <stream.h>
void pr(int i)	// same pr() as before
{
    cout << chr(i) << " " << i << "\n";
}

int main(int, char**)
{
    for (int i = 040; i <= 0176; i++)
	pr(i);
    return 0;
}


//: print all printable characters portably
// version 2
#include <limits.h>
#include <stream.h>
#include <ctype.h>

void pr(int i)	// same pr() as before
{
    cout << chr(i) << " " << i << "\n";
}

int main(int, char**)
{
    for (int i = 0; i <= CHAR_MAX; i++)
	if (isprint(i))
	    pr(i);
    return 0;
}
*******************************************************************

//: print a number as a char and its hex value
void pr(int i)
{
    cout << chr(i) << " " << hex(i) << "\n";
}
*******************************************************************



Menu of Chapter 2 Answers 
Answer to Question 2.6