Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

by c++ language Instructions: This is pair work Write a program that will read a

ID: 3749587 • Letter: B

Question

by c++ language

Instructions: This is pair work Write a program that will read a line of text and output a list of all the letters that occur in the text together with the number of times each letter occurs in the line. End the line with a period that serves as a sentinel value. The letters should be listed in the following order: the most frequently occurring letter, the next most frequently occurring letter, and so forth. Use two arrays, one to hold integers and one to hold letters. You may assume that the input uses all lowercase letters. For example, the input. do be do bo should produce output similar to the following Letter Number of Occurrences Figure 1

Explanation / Answer

#include <iostream>

using namespace std;

int main() {

string phrase;

cout << "Enter: ";

getline(cin, phrase);

char letter[27] = "abcdefghijklmnopqrstuvwxyz";

int count[26] = {0};

for(int i=0; i<phrase.length(); i++)

{

if(phrase[i] == '.')

break;

if(phrase[i] != ' ')

count[(int)phrase[i] - 97]++;

}

cout << endl;

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

{

for(int j=i; j<26; j++)

{

if(count[i] < count[j])

{

int temp = count[i];

count[i] = count[j];

count[j] = temp;

char t = letter[i];

letter[i] = letter[j];

letter[j] = t;

}

}

}

for(int i=0; count[i]!= 0; i++)

{

cout << letter[i] << " - " << count[i] << endl;

}

}

/*SAMPLE OUTPUT

Enter: do be do bo.

o - 3

d - 2

b - 2

e - 1

*/