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; }
Max Function
Statistical function is of many categories. One of them is a MAX function. The MAX function returns the largest value from the list of arguments passed to it. MAX function always ignores the empty cells when performing the calculation.
Power Function
A power function is a type of single-term function. Its definition states that it is a variable containing a base value raised to a constant value acting as an exponent. This variable may also have a coefficient. For instance, the area of a circle can be given as:
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.
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:
To add {key, value} pairs to the map, you can use the insert member function:
Then you can access elements with the at member function, for example:
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.
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:
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; | |
} |
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 6 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
![C++ for Engineers and Scientists](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
![C++ for Engineers and Scientists](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
![EBK JAVA PROGRAMMING](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)