// 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
int A(0), a(0), B(0), b(0), C(0), c(0), D(0), d(0), E(0), e(0), F(0), f(0), G(0), g(0), H(0), h(0), I(0), i(0), J(0), j(0), K(0), k(0), L(0), l(0), M(0), m(0),
N(0), n(0), O(0), o(0), P(0), p(0), Q(0), q(0), R(0), r(0), S(0), s(0), T(0), t(0), U(0), u(0), V(0), v(0), W(0), w(0), X(0), x(0), Y(0), y(0), Z(0), z(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 ...
cout << inputCharacter ;
switch ( inputCharacter )
{
case 'A': ++ A ; break; case 'a': ++ a ; break;
case 'B': ++ B ; break; case 'b': ++ b ; break;
case 'C': ++ C ; break; case 'c': ++ c ; break;
case 'D': ++ D ; break; case 'd': ++ d ; break;
case 'E': ++ E ; break; case 'e': ++ e ; break;
case 'F': ++ F ; break; case 'f': ++ f ; break;
case 'G': ++ G ; break; case 'g': ++ g ; break;
case 'H': ++ H ; break; case 'h': ++ h ; break;
case 'I': ++ I ; break; case 'i': ++ i ; break;
case 'J': ++ J ; break; case 'j': ++ j ; break;
case 'K': ++ K ; break; case 'k': ++ k ; break;
case 'L': ++ L ; break; case 'l': ++ l ; break;
case 'M': ++ M ; break; case 'm': ++ m ; break;
case 'N': ++ N ; break; case 'n': ++ n ; break;
case 'O': ++ O ; break; case 'o': ++ o ; break;
case 'P': ++ P ; break; case 'p': ++ p ; break;
case 'Q': ++ Q ; break; case 'q': ++ q ; break;
case 'R': ++ R ; break; case 'r': ++ r ; break;
case 'S': ++ S ; break; case 's': ++ s ; break;
case 'T': ++ T ; break; case 't': ++ t ; break;
case 'U': ++ U ; break; case 'u': ++ u ; break;
case 'V': ++ V ; break; case 'v': ++ v ; break;
case 'W': ++ W ; break; case 'w': ++ w ; break;
case 'X': ++ X ; break; case 'x': ++ x ; break;
case 'Y': ++ Y ; break; case 'y': ++ y ; break;
case 'Z': ++ Z ; break; case 'z': ++ z ; break;
} // end switch
} // 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(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "B , b" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "C , c" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "D , d" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "E , e" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "F , f" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "G , g" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "H , h" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "I , i" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "J , j" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "K , k" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "L , l" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "M , m" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "N , n" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "O , o" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "P , p" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "Q , q" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "R , r" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "S , s" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "T , t" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "U , u" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "V , v" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "W , w" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "X , x" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "Y , y" << setw(13) << setw(13) << setw(16) << endl;
cout << setw(7) << "Z , 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 |