C++11, please compile before posting answer to make sure there aren\'t errors. t
ID: 3667951 • Letter: C
Question
C++11, please compile before posting answer to make sure there aren't errors. thank you!
The program should print a string of text to the terminal before getting each line of input from the user. A session should look like one of the following examples (including whitespace and formatting), with a possibly numbers and letters in the output:
The string printed by the program should include a newline at the end, but no other trailing whitespace (whitespace at the end of the line)
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. Case should be ignored ('a' should be treated the same as A'). If two or more letters have same frequency, print them in lexicographical order. The program will prompt the user to enter the lines of text in the terminal The text line may contain any English letter and zero or more whitespace characters (spaces and tabs only) For example, the input do be do bo. should produce output similar to the following: Letter of Occurrences Your program should include at least one function that takes in an array. Hint: You may want to use this function to sort the letters and their frequenciesExplanation / Answer
#include <iostream>
#include <string>
using namespace std;
int main()
{
char string[100];
int c = 0, count[26] = {0};
cout<<"Enter text: "<< endl;
cin.getline(string,sizeof(string));
while (string[c] != '')
{
/** Considering characters from 'a' to 'z' only
and ignoring others */
if (string[c] >= 'a' && string[c] <= 'z')
count[string[c]-'a']++;
c++;
}
cout<<"Frequencies: ";
for (c = 0; c < 26; c++)
{
/** Printing only those characters
whose count is at least 1 */
if (count[c] != 0)
cout<<static_cast<char>(c + 'a')<<" "<<count[c]<<" ";
}
return 0;
}
Out Put:
Enter text:
www erer trett
Frequencies:
e 3
r 3
t 3
w 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.