Sentence Statistics Create a program that can tell a user how many times a particular character appears in text input. Your program will analyze the input, then prompt the user to enter characters to get stats. When the user types 'Q' the program will exit. Enter text input to analyze: The quick brown fox jumped over the lazy dog. What character do you want stats on? a The character a appears 1 times. What character do you want stats on? e The character e appears 4 times. What character do you want stats on? z The character z appears 1 times. What character do you want stats on? Q Goodbye! Analyzing the sentence We've provided starter code in main.cc to get input from the user. Now you need to analyze it. You can use a std::map to store information about each character and the number of times it appears in the input. A std::map maps keys of one type to values of another type. For this problem, you'll want to map from a letter to the number of times that letter appears. In C++, the char keyword is used to declare character type variables. A character variable can store only a single character. In this case, your keys could be std::string or char. to represent a letter, and your values could be integers, to represent the number of times that letter appears in the input. To declare a new map of char to int, for example, you could do: std::map my_map; To add {key, value} pairs to the map, you can use the insert member function: // Insert the key 'c' into my_map with value 1 my_map.insert({'c', 1}); Then you can access elements with the at member function, for example: // Get the value for key 'c' int value = my_map.at('c'); Note that if you try to access an element with at but that element doesn't exist in the map, the program will crash. You can prevent this is by checking if a key exists in a map first. One way to do this is by using the count member function, which returns how many times a given key appears in the map. If it returns 0, then it means that the given key is not in the map. if (my_map.count('c') == 0) { // The letter 'c' is not currently a key in the map. } Responding to the user Once you have your map of character counts, you can enter a loop that will exit only when the user types 'Q'. We will use a loop to get characters from the user and tell them how many times each character appears in the sentence. We've provided this loop for you, and we do so by using a while loop, which functions similarly to the for loops you've already learned about. The syntax for a C++ while loop is: while (condition) { // code block to be executed } The code block in a while loop will be executed as long as the condition is true. In this problem, using while (true) ensures that we will keep asking the user for input as many times as they want - and we can exit out of the loop on demand, using a special keyword called break, which is used to jump out of a loop.   main.cc file #include   #include       int main() {   std::string text;   std::cout << "Enter text to analyze: ";   // std::getline allows us to capture an entire line of input   // and store it into the text variable.   std::getline(std::cin, text);       // ===================== YOUR CODE HERE =====================   // 1. Construct a map to map from each character to its   // frequency in the inputted line of text.   // Don't forget to #include .   // 2. Write a loop to iterate through each character in the   // text and insert to (or update) the map to track that   // character's frequency. We suggest using my_map.count   // to check if a character is not yet in the map, see   // the README for an example.   // Hint: you can use a range based for loop to iterate over   // characters in a string: for (char c : text) {...}   // ==========================================================       // The code block in a while loop will be executed as long as   // the condition in the parentheses is true. We use `while (true)`   // so we can ask the user for input as many times as they want.   char input = 'Q';   while (true) {   std::cout << "What character do you want stats on? ";   std::cin >> input;   // The special keyword "break" is used to jump out of a loop.   // Since the while loop's condition is always true, we will   // only stop looping when the user inputs 'Q'.   if (input == 'Q') {   break;   }       // =============== YOUR CODE HERE ===============   // Use the map you constructed to access   // the frequency of the requested character.   // Show the user how many times the character   // appeared.   //   // Hint: don't forget to check if   // the given character exists in your map first,   // and print out 0 if it is not in your map.   // ==============================================   }   std::cout << "Goodbye!" << std::endl;   return0;   }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter5: Control Structures Ii (repetition)
Section: Chapter Questions
Problem 28PE
icon
Related questions
Question

Sentence Statistics

Create a program that can tell a user how many times a particular character appears in text input. Your program will analyze the input, then prompt the user to enter characters to get stats. When the user types 'Q' the program will exit.

Enter text input to analyze: The quick brown fox jumped over the lazy dog. What character do you want stats on? a The character a appears 1 times. What character do you want stats on? e The character e appears 4 times. What character do you want stats on? z The character z appears 1 times. What character do you want stats on? Q Goodbye!

Analyzing the sentence

We've provided starter code in main.cc to get input from the user. Now you need to analyze it. You can use a std::map to store information about each character and the number of times it appears in the input.

A std::map maps keys of one type to values of another type. For this problem, you'll want to map from a letter to the number of times that letter appears.

In C++, the char keyword is used to declare character type variables. A character variable can store only a single character. In this case, your keys could be std::string or char. to represent a letter, and your values could be integers, to represent the number of times that letter appears in the input. To declare a new map of char to int, for example, you could do:

std::map<char, int> my_map;

To add {key, value} pairs to the map, you can use the insert member function:

// Insert the key 'c' into my_map with value 1 my_map.insert({'c', 1});

Then you can access elements with the at member function, for example:

// Get the value for key 'c' int value = my_map.at('c');

Note that if you try to access an element with at but that element doesn't exist in the map, the program will crash. You can prevent this is by checking if a key exists in a map first. One way to do this is by using the count member function, which returns how many times a given key appears in the map. If it returns 0, then it means that the given key is not in the map.

if (my_map.count('c') == 0) { // The letter 'c' is not currently a key in the map. }

Responding to the user

Once you have your map of character counts, you can enter a loop that will exit only when the user types 'Q'. We will use a loop to get characters from the user and tell them how many times each character appears in the sentence.

We've provided this loop for you, and we do so by using a while loop, which functions similarly to the for loops you've already learned about. The syntax for a C++ while loop is:

while (condition) { // code block to be executed }

The code block in a while loop will be executed as long as the condition is true. In this problem, using while (true) ensures that we will keep asking the user for input as many times as they want - and we can exit out of the loop on demand, using a special keyword called break, which is used to jump out of a loop.

 

main.cc file

#include <iostream>
  #include <string>
   
  int main() {
  std::string text;
  std::cout << "Enter text to analyze: ";
  // std::getline allows us to capture an entire line of input
  // and store it into the text variable.
  std::getline(std::cin, text);
   
  // ===================== YOUR CODE HERE =====================
  // 1. Construct a map to map from each character to its
  // frequency in the inputted line of text.
  // Don't forget to #include <map>.
  // 2. Write a loop to iterate through each character in the
  // text and insert to (or update) the map to track that
  // character's frequency. We suggest using my_map.count
  // to check if a character is not yet in the map, see
  // the README for an example.
  // Hint: you can use a range based for loop to iterate over
  // characters in a string: for (char c : text) {...}
  // ==========================================================
   
  // The code block in a while loop will be executed as long as
  // the condition in the parentheses is true. We use `while (true)`
  // so we can ask the user for input as many times as they want.
  char input = 'Q';
  while (true) {
  std::cout << "What character do you want stats on? ";
  std::cin >> input;
  // The special keyword "break" is used to jump out of a loop.
  // Since the while loop's condition is always true, we will
  // only stop looping when the user inputs 'Q'.
  if (input == 'Q') {
  break;
  }
   
  // =============== YOUR CODE HERE ===============
  // Use the map you constructed to access
  // the frequency of the requested character.
  // Show the user how many times the character
  // appeared.
  //
  // Hint: don't forget to check if
  // the given character exists in your map first,
  // and print out 0 if it is not in your map.
  // ==============================================
  }
  std::cout << "Goodbye!" << std::endl;
  return0;
  }
Expert Solution
Given Code:

Computer Science homework question answer, step 1, image 1

Computer Science homework question answer, step 1, image 2

Computer Science homework question answer, step 1, image 3

 

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 6 images

Blurred answer
Knowledge Booster
Mathematical functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT