'>Write a program, using 25 or fewer lines of code, to count each occurrence of each lower-case letter in a string. Start your program by declaring a character array, msg, with a size of 30 of greater.
 char msg[MAX]; 
The user should then type a text string into the array:
 cin.getline(msg, MAX); 
Your program should now report the number of each character (a-z) found in the array: a face at the beach a – 4 b – 1 c – 2 d – 0 e – 3 f – 1 g – 0 h – 2 i – 0 j – 0 k – 0 l – 0 m – 0 n – 0 o – 0 p – 0 q – 0 r – 0 s – 0 t – 2 u – 0 v – 0 w – 0 x – 0 y – 0 z – 0 Here is one way of creating that output using characters in a loop:
 for (i=0; i<26; i++) cout << static_cast(i + 'a') << " - " << etc.. 

In order to count each occurrence of each lower-case letter in a string using 25 or fewer lines of code, we can use an array to keep track of the count for each letter.

First, we declare a character array called `msg` with a size of at least 30. The user is then prompted to type a text string into the array using `cin.getline(msg, MAX);`.

To count the occurrences of each letter, we will initialize an array called `count` with 26 elements, where each element represents a letter from ‘a’ to ‘z’. We will initialize all elements to 0. This can be done using `int count[26] = {0};`.

Next, we iterate over each character in the `msg` array. For each lowercase letter encountered, we increment the corresponding element in the `count` array. This can be achieved with a for loop that goes from 0 to the length of the string:

“`cpp
for (int i = 0; msg[i]; i++) {
if (msg[i] >= ‘a’ && msg[i] <= 'z') { count[msg[i] - 'a']++; } } ``` Inside the loop, we check if the character is a lowercase letter using `msg[i] >= ‘a’ && msg[i] <= 'z'`. If it is, we subtract 'a' from the character to get the index of the corresponding element in the `count` array. We then increment that element by 1. After counting the occurrences, we can print out the results. We iterate over the `count` array and print the number of occurrences for each letter. We can use another for loop for this: ```cpp for (int i = 0; i < 26; i++) { cout << static_cast(i + ‘a’) << " - " << count[i] << endl; } ``` Inside the loop, we use `static_cast(i + ‘a’)` to convert the index back to the corresponding lowercase letter. We then print the letter, followed by a hyphen and the count.

Putting it all together, the complete program would look like this:

“`cpp
#include
using namespace std;

int main() {
const int MAX = 30;
char msg[MAX];
int count[26] = {0};

cout << "Enter a text string: "; cin.getline(msg, MAX); for (int i = 0; msg[i]; i++) { if (msg[i] >= ‘a’ && msg[i] <= 'z') { count[msg[i] - 'a']++; } } cout << "Occurrences of each letter:" << endl; for (int i = 0; i < 26; i++) { cout << static_cast(i + ‘a’) << " - " << count[i] << endl; } return 0; } ``` This program will count the occurrences of each lowercase letter in the input string and display the results. It does so in fewer than 25 lines of code.

Need your ASSIGNMENT done? Use our paper writing service to score better and meet your deadline.


Click Here to Make an Order Click Here to Hire a Writer