C++ Problem: Counting the Occurrences of Each Letter

This section presents a program to count the occurrences of each letter in an array of characters.

The program does the following:

  1. Generate 100 lowercase letters randomly and assign them to an array of characters, as shown in Figure 7.5a. As discussed in Section 4.4, “Case Study: Generating Random Characters,” a random lowercase letter can be generated using

static_cast<char>(‘a’ + rand() % (‘z’ – ‘a’ + 1))

  1. Count the occurrences of each letter in the array. To do so, declare an array, say counts of 26 int values, each of which counts the occurrences of a letter, as shown in Figure 7.5b. That is, counts[0] counts the number of a’s, counts[1] counts the number of b’s, and so on.

Figure 7.5 The chars array stores 100 characters and the counts array stores 26 counts, each counting the occurrences of a letter.

Listing 7.8 gives the complete program.

Listing 7.8 CountLettersInArray.cpp

1 #include <iostream>
2
#include <ctime>
3
using namespace std;
4
5
const int NUMBER_OF_LETTERS = 26;
6
const int NUMBER_OF_RANDOM_LETTERS = 100;
7
void createArray(char []);
8
void displayArray(const char []);
9
void countLetters(const char [], int []);
10
void displayCounts(const int []);
11
12
int main()
13 {
14   
// Declare and create an array
15    char chars[NUMBER_OF_RANDOM_LETTERS];
16
17   
// Initialize the array with random lowercase letters
18    createArray(chars);
19
20   
// Display the array
21    cout << “The lowercase letters are: ” << endl;
22    displayArray(chars);

23
24   
// Count the occurrences of each letter
25    int counts[NUMBER_OF_LETTERS];
26
27   
// Count the occurrences of each letter
28    countLetters(chars, counts);
29
30   
// Display counts
31    cout << “\nThe occurrences of each letter are: ” << endl;
32    displayCounts(counts);
33
34   
return 0;
35 }
36
37
// Create an array of characters
38 void createArray(char chars[])
39 {
40   
// Create lowercase letters randomly and assign
41    // them to the array
42    srand(time(0));
43   
for (int i = 0; i < NUMBER_OF_RANDOM_LETTERS; i++)
44    chars[i] =
static_cast<char>(‘a’ + rand() % (‘z’ ‘a’ + 1));
45 }
46
47
// Display the array of characters
48 void displayArray(const char chars[])
49 {
50   
// Display the characters in the array 20 on each line
51    for (int i = 0; i < NUMBER_OF_RANDOM_LETTERS; i++)
52    {
53       
if ((i + 1) % 20 == 0)
54           cout << chars[i] <<
” ” << endl;
55       
else
56           cout << chars[i] << ” “;
57     }
58 }
59
60
// Count the occurrences of each letter
61 void countLetters(const char chars[], int counts[])
62 {
63     
// Initialize the array
64    for (int i = 0; i < NUMBER_OF_LETTERS; i++)
65    counts[i] =
0;
66
67   
// For each lowercase letter in the array, count it
68    for (int i = 0; i < NUMBER_OF_RANDOM_LETTERS; i++)
69    counts[chars[i] –
‘a’] ++;
70 }
71
72
// Display counts
73 void displayCounts(const int counts[])
74 {
75   
for (int i = 0; i < NUMBER_OF_LETTERS; i++)
76    {
77       
if ((i + 1) % 10 == 0)
78           cout << counts[i] <<
” ” << static_cast<char>(i + ‘a’) << endl;
79       
else
80           cout << counts[i] << ” ” << static_cast<char>(i + ‘a’) << ” “;
81    }
82 }

The createArray function (lines 38-45) generates an array of 100 random lowercase letters and assigns them in array chars. The countLetters function (lines 61-70) counts the occurrence of letters in chars and stores the counts in the array counts. Each element in counts stores the number of occurrences of a letter. The function processes each letter in the array and increases its count by one. A brute force approach to count the occurrences of each letter might be as follows:

for (int i = 0; i < NUMBER_OF_RANDOM_LETTERS; i++)

if (chars[i] == ‘a’)

counts[0]++;

else if (chars[i] == ‘b’)

counts[1]++;

But a better solution is given in lines 68-69.

for (int i = 0; i < NUMBER_OF_RANDOM_LETTERS; i++)

counts[chars[i] – ‘a’]++;

If the letter (chars[i]) is ‘a’, the corresponding count is counts[‘a’ –   ‘a’] (i.e., counts[0]). If the letter is ‘b’, the corresponding count is counts[‘b’ – ‘a’] (i.e., counts[1]) since the ASCII code of ‘b’ is one more than that of ‘a’. If the letter is ‘z’, the corresponding count is counts[‘z’ – ‘a’] (i.e., counts[25]) since the ASCII code of ‘z’ is 25 more than that of ‘a’.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *