Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The instruction in the textbook: 7.37 (Count occurrence of each letter in a string) Write a function that counts the occurrence of each letter in

The instruction in the textbook: 7.37 (Count occurrence of each letter in a string) Write a function that counts the occurrence of each letter in the string using the following header:

void count(const char s[], int counts[])

where counts is an array of 26 integers. counts[0], counts[1],...., and counts [25] count the occurrence of a,b,...., and z, respectively. Letters are not case-sensitive, i.e., letter A and a counted the same as a.

Write a test program that reads a string, invokes the count function, and displays the non-zero counts. Here is a sample run of the program:

Enter a string: Welcome to New York! [Enter]

c: 1 times

e: 3 times

k: 1 times

l: 1 times

m: 1 times

n: 1 times

0: 3 times

r: 1 times

t: 1 times

w: 2 times

y: 1 times

#include #include #include using namespace std;

//functions are to be declared before defined. If you put the entire code after main() before the main() then these function //declarations are not necessary. If coding after main() this is needed! void Prog7_1(); void Prog7_5(); void Prog7_8(); void Prog7_11(); void Prog7_16(); void Prog7_26(); void Prog7_32(); void Prog7_36(); void Prog7_37(); void Prog7_41();

int main() { while (true) { system("cls"); cout << " Main Menu - Chapter 7 "; cout << "======================================== "; cout << "Programming Exercise 7.1 "; cout << "Programming Exercise 7.5 "; cout << "Programming Exercise 7.8 "; cout << "Programming Exercise 7.11 "; cout << "Programming Exercise 7.16 "; cout << "Programming Exercise 7.26 "; cout << "Programming Exercise 7.32 "; cout << "Programming Exercise 7.36 "; cout << "Programming Exercise 7.37 "; cout << "Programming Exercise 7.41 "; cout << "other: Exit "; cout << "======================================= "; cout << "Enter an exercise: ";

int input; cin>>input; switch (input) { case 1: Prog7_1(); break; case 5: Prog7_5(); break; case 8: Prog7_8(); break; case 11: Prog7_11(); break; case 16: Prog7_16(); break; case 26: Prog7_26(); break; case 32: Prog7_32(); break; case 36: Prog7_36(); break; case 37: Prog7_37(); break; case 41: Prog7_41(); break; default: exit(0); } cout << endl; system("pause");

cin.clear(); }

return 0; }

void count (const char s[], int counts[]) { //logic of the program starts here int j; int k; // it is just unreferenced //int l;

char it = 'a'; for ( int i = 0; i < strlen(s); i++) /////////////////////////////// compiler does not like, but its is only a warning. { //uses the ascii values of a to z and A to Z for (j = 97, k = 65; j <= 122, k <= 90; j++, k++) { //compare whether each element of s[i] is equal to the small letter or capital letter if ((s[i] == j) || (s[i] == k)) { //subtract 97 to start the indexing from 0 counts [j - 97]++; } } } //print the result for (int i = 0; i <= 25; i++ ) { if (counts[i] > 0) { cout << char( it + i )<< " : " << counts[i] << " times " << endl; } } }

void Prog7_37() { const int size = 50; char s [size]; //initialize all elements to zero int counts [26] = {}; cout << "Enter a string of letter (a...z): "; cin.ignore(); cin.getline(s, 50, ' ');

// call the function count (s, counts);

//function protoype }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Jay Greenspan, Brad Bulger

1st Edition

978-0764535376

More Books

Students also viewed these Databases questions

Question

2. Why has the conflict escalated?

Answered: 1 week ago

Question

1. What might have led to the misinformation?

Answered: 1 week ago