Amount of each letters in a book

The title sums it up. I need to know how many letters of each letter, uppercase and lowercase are in a book. This is what I have now. I need it so it doesn't print out the whole book but just the amount of each letters. Just steer me in the right direction, this is a homework assignment.
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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cctype>

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 ( 0 ) ;
int totalNumberOfUppercaseLetters ( 0 ) ;
int numberOfAs ( 0 ) ;
// This function reads and counts the number of occurance of the various letters
void readAndCountLetters ( ifstream & inputStream )
{
	 char      inputCharacter ;	
	 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 << endl;
cout << "The total number of letters  is " << totalNumberOfLetters << endl;	
cout << "The total number of lowercase letters  is " << totalNumberOfLowercaseLetters << endl;	 
cout << "The total number of uppercase letters  is " << totalNumberOfUppercaseLetters << endl; 
cout << "The total number of A's  are " << numberOfAs << 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 
Disregard the "numberOfAs" part. Just me messing around trying things out.
Use arrays to keep track of the letter counts.
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
// ...
int uppercaseLetterCounts[26] = {0};
int lowercaseLetterCounts[26] = {0};

int getCountOfLetter(char x)
{
    if(std::isupper(x))
    {
        return uppercaseLetterCounts[x - 'A'];
    }
    else if (std::islower(x))
    {
        return lowercaseLetterCounts[x - 'a'];
    }
    else
    {
        return -1;
    }
}

int getTotalNumberOfUppercaseLetters()
{
    int total = 0;
    for(int i = 0; i < 26; ++i)
    {
        total += uppercaseLetterCounts[i];
    }
    return total;
}

int getTotalNumberOfLowercaseLetters()
{
    int total = 0;
    for(int i = 0; i < 26; ++i)
    {
        total += lowercaseLetterCounts[i];
    }
    return total;
}

int getTotalNumberOfLetters()
{
    int totalNum = 0;
    totalNum += getTotalNumberOfLowercaseLetters();
    totalNum += getTotalNumberOfUppercaseLetters();
    return totalNum;
}

void readAndCountLetters(std::istream& inputStream)
{
    char inputChar;
    while(inputStream >> inputChar)
    {
        if(std::isupper(inputChar)) ++uppercaseLetterCounts[inputChar - 'A'];
        else if(std::islower(inputChar)) ++lowercaseLetterCounts[inputChar - 'a'];
    }
}
// ... 
could someone dumb this down for me? I'm new to c++ and not really getting what to do.
@booradley60
Giving solutions to the main part of a problem is a faux-pas on this forum.

@Joshcannon
At this point in the class, if you've been given this as homework, you've probably been taught arrays (or are at least expected to know about them). If you don't know about arrays, here's a handy link:
http://www.cplusplus.com/doc/tutorial/arrays/

Instead of using 52 different variables, you can merely use an array that's 52 elements long (or, better yet, two arrays that are 26 characters long). In your head, map each element of each array to a different letter (most of us go down the alphabet), then figure out which elements will represent uppercase letters and which represent lowercase letters. Which path you choose will impact how your code gets implemented.

I'm assuming the assignment asked you to be case-sensitive because your code takes case into account. If it's supposed to be case-insensitive, use one array that's 26 elements long.

Using arrays makes life a lot easier since you can just use a loop to iterate down the length of your array(s) for output, and if you know what you're doing, you can completely do away with a long switch statement.

Fun little tip: each character has an equivalent integral value. For example, the lowercase letter 'a' will map to 97. The uppercase letter 'A' will map to 65. Here's a table if you're interested. Note how all the letters have consecutive values.
http://www.asciitable.com/

-Albatross
Last edited on
Giving solutions to the main part of a problem is a faux-pas on this forum
Yes, I am aware.

your solution ignores the fact that the OP's code is case-sensitive.
How so?
Last edited on
Whoops, wait, it doesn't. My bad.

-Albatross
Last edited on
Sorry for bailing on you OP, I had to go to bed last night. Basically I wanted to start with a big (kinda scary maybe) solution and whittle it down to where you understood the concepts, instead of trying to move your code iteratively toward a solution. I'm lazy.

Basically, what I posted uses what Albatross is suggesting. You make the code a lot more tidy by using arrays than by using individual letter counts. Where Albatross mentions 'mapping', those are the parts where I'm doing x - 'A' or x - 'a'.

If I have an array of lowercase letter counts, that means I have an array of 26 items, one item per letter. This array is indexed by numbers 0-25 (lowercaseLetterCount[0]...lowercaseLetterCount[25]), but the alphabet goes from 'a' to 'z'.

When iterating over the input stream, lets say I encounter the letter 'c'. What happens is I find the appropriate index by saying 'c' - 'a' which equals 2. If I say lowercaseLetterCount['c' - 'a'], that's the same as saying lowercaseLetterCount[2]. If I have access to the integer at this index, I can increment it by one in order to express that I have encountered another letter 'c'. lowercaseLetterCount[2]++;

Same goes for uppercase letters. The reason I used two separate arrays is because the uppercase and lowercase letters don't have consecutive codes. (There's a gap between uppercase 'Z' and lowercase 'a' where the ASCII codes indicate things like square brackets and underscores). I think it made the mapping a little more intuitive and expressive using separate arrays, but it is possible to do them all in one.
Last edited on
Urgent!
I have the table made out at the very end of compiling it. How would I remove all the text and get the letters to count. I'm still not getting the Array stuff yet. I would of posted earlier but had work for most of the day. Someone please help.
// 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
Basically an array is like this:

int a[size];

where size is the amount of space you are telling the compiler to reserve in memory. Say for example you have a size of 5 that means in memory it looks like this
[ ][ ][ ][ ][ ] (5 places in memory reserved for this array)

They start at index 0 and work up ( 0 since that is the offset from the start of reserved memory )

so if we assign a value of 5 to element 4 ( index 3 )

we would do this like

a[3] = 5;

now in memory it looks like this

[ ][ ][ ][5][ ]
okay, i have the array part down, but how would i get my printTable function to print out the letters? here's what i have now.
#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
could anyone please help me?
Topic archived. No new replies allowed.