1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
// Adam Horn October 30th, 2013 Computer Science I
//This program will print out the number of uppercase, lowercase, total number and the percent of that letter over the entire book of "Alice"
//This will do it for every letter in the alphabet and print out totals of all the letters.
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cctype>
#include <iomanip>
using namespace std ;
// declare global variables
char numberOf[128] = { 0 } ;
int totalNumberOfLetters ( 0 ) ;
int totalNumberOfLowercaseLetters ;
int totalNumberOfUppercaseLetters ;
// This function reads and counts the number of occurance of the various letters
void readAndCountLetters ( ifstream & inputStream )
{
char inputCharacter ;
int total = 0;
while ( ! inputStream.eof() )
{
// read one character from input file ...
inputStream.get ( inputCharacter ) ;
//if ( isalpha (inputCharacter) )
//totalNumberOfLetters ++ ;
if ( ( ('A' <= inputCharacter ) && (inputCharacter <= 'Z' ) ) || (('a'<= inputCharacter) && (inputCharacter <= 'z')) )
totalNumberOfLetters ++ ;
if ( isupper (inputCharacter) )
totalNumberOfUppercaseLetters ++ ;
if ( islower (inputCharacter) )
totalNumberOfLowercaseLetters ++ ;
// and now print the character just read ...
numberOf [ inputCharacter ] ;
} // end while loop
} // end function readAndCountLetters
void printTable ( )
{
cout << endl;
cout << setw(17) << "Letter" << setw(13) << "Uppercase" << setw(13) << "Lowercase" << setw(16) << "Percentage of" << endl;
cout << setw(7) << "Letter" << setw(10) << "Count" << setw(13) << "Count" << setw(13) << "Count" << setw(16) << "All Letters" << endl;
cout <<setw(7) << "------" << setw(10) << "-------" << setw(13) << "----------" << setw(13) << "----------" << setw(16) << "-------------" << endl;
cout << endl;
cout << setw(7) << "A , a" << setw(10) << 'A' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "B , b" << setw(10) << 'B' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "C , c" << setw(10) << 'C' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "D , d" << setw(10) << 'D' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "E , e" << setw(10) << 'E' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "F , f" << setw(10) << 'F' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "G , g" << setw(10) << 'G' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "H , h" << setw(10) << 'H' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "I , i" << setw(10) << 'I' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "J , j" << setw(10) << 'J' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "K , k" << setw(10) << 'K' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "L , l" << setw(10) << 'L' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "M , m" << setw(10) << 'M' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "N , n" << setw(10) << 'N' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "O , o" << setw(10) << 'O' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "P , p" << setw(10) << 'P' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "Q , q" << setw(10) << 'Q' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "R , r" << setw(10) << 'R' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "S , s" << setw(10) << 'S' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "T , t" << setw(10) << 'T' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "U , u" << setw(10) << 'U' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "V , v" << setw(10) << 'V' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "W , w" << setw(10) << 'W' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "X , x" << setw(10) << 'X' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "Y , y" << setw(10) << 'Y' << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "Z , z" << setw(10) << 'Z' << setw(13) << setw(13) << setw(16) << endl;
cout <<setw(7) << "------" << setw(10) << "-------" << setw(13) << "----------" << setw(13) << "----------" << setw(16) << "-------------" << endl;
cout << endl;
cout <<setw(7) << "Totals" << setw(10) << totalNumberOfLetters << setw(13) << totalNumberOfUppercaseLetters << setw(13) << totalNumberOfLowercaseLetters << setw(16) << "100" << endl;
} // end printTable
int main ( )
{
// declare variables ...
ifstream myInputStream ;
// attempt to open input text file ...
myInputStream.open ( "Alice.txt" ) ;
// test whether opening of input file failed ...
if ( myInputStream.fail() )
{
cout << "\nFailed to open input file 'Alice.txt'.\n";
exit(1) ;
}
// now calling function to read and count letters
readAndCountLetters ( myInputStream ) ;
// close the input file ...
myInputStream.close () ;
printTable ( );
} // end main function
|