Step Seven  Alpha-Only

Design this program called alphaonly.cpp to accept the input of one character value and output whether it is an alphabetical character or not. This time we will use ASCII values to avoid REALLY long "if" or "switch" expressions since there are exactly 52 alphabetic characters (remember upper and lower case?)

The ASCII numbers are how characters are stored internally in a computer. For example, capital A is 65, B is 66 and so on in order. Also notice that the lower case letters are 32 more than the corresponding upper case letters. The invention of this standard is primarily credited to Bob Bemer who passed away in 2004.

if (letter >= 'A' && letter <= 'Z') {
  cout << "alphabetical!" << endl;
else if (letter >= 'a' && letter <= 'z') {
  cout << "alphabetical!" << endl;
else {
  cout << "not alphabetical!" << endl;

It may be more exciting to create a function which will give you a value of true if the character is alphabetical and false otherwise. You could do that something like this:

bool isalpha(char ch) {
  if ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') )
    return true;
  else return false;
This function will have to be typed in above the "main" function. Then the "main" function might look like this:
int main(void) 
  int alphacounter = 0;
  char letter;
  while (cin.get(letter)) {
    if ( isalpha(letter) ) ++alphacounter;
  //now display the results
  cout << "There were " << alphacounter << " alphabetic characters." << endl;
Now to see how many alphabetic characters are in the Gettysburg Address, you can do something like the previous lesson:

./alphaonly < gettysburg.txt

If you add the code from the previous lesson, you can now correctly count both the number of vowels and consonants.

Advance to Step Eight

Return to the Table of Contents