Question

Write a program in C++ to read a large piece of text from the user (multiple...

Write a program in C++ to read a large piece of text from the user (multiple lines) until the user inputs EOF. Assume the input only contains alphabets, numbers, commas and periods. The program should first compute the number of occurrences of each character in the input. Next, the program displays a sorted list (in descending order) of each character and the corresponding number of occurrences of that character. The sorting should be performed on the counts and not the characters.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go this code and let me know if u need any changes in this.Thank You
_________________

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
//Declaring variables
string str;
int val;
char chars[26];
int freq[26] = { 0 };

for (int i = 0; i < 26; i++) {
chars[i] = (char)(65 + i);
}
//Getting the input entered by the user until the user enters EOF
cout << "Enter the String :";
cin >> str;
while (str.compare("EOF") != 0) {
for (int i = 0; i < str.length(); i++) {
char ch = str.at(i);
if (ch >= 'a' && ch <= 'z') {
val = ch - 97;
freq[val]++;
}
else if (ch >= 'A' && ch <= 'Z') {
val = ch - 65;
freq[val]++;
}
}

cin >> str;
}

//This Logic will Sort the Array of elements in Decending order
int temp1;
char temp;
for (int i = 0; i < 26; i++) {
for (int j = i + 1; j < 26; j++) {
if (freq[i] < freq[j]) {
temp1 = freq[i];
freq[i] = freq[j];
freq[j] = temp1;

temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
}
}

//Displaying the output
cout << " Character Frequency" << endl;
cout << "--------- ---------" << endl;

for (int i = 0; i < 26; i++) {
cout << chars[i] << " " << freq[i] << endl;
}

return 0;
}

______________________

Output:

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Write a program in C++ to read a large piece of text from the user (multiple...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LANGUAGE = C i. Write a program that takes int numbers from user until user gives...

    LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...

  • Question 2 Write a program that will read in a line of text up to 100...

    Question 2 Write a program that will read in a line of text up to 100 characters as string, and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, an<d periods. When...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • c program that counts the number of characters, words and lines from standard input until EOF....

    c program that counts the number of characters, words and lines from standard input until EOF. attached is what i Have so far but its not working ?. about shell redirection Requirements 1. Write a C program that counts the number of characters, words and lines read from standard Input until EOF Is reached. 2. Assume the Input is ASCII text of any length. 3. Every byte read from stdin counts as a character except EOF 4. Words are defined...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

  • Write a C program that counts the number of each digit entered by the user (Input...

    Write a C program that counts the number of each digit entered by the user (Input process should be ended using EOF character). You should use switch to compute the number of each digits. For each digit your program should print a line containing that number of adjacent asterisks. Sample Input1: 18218342850056D855599 Sample Outputi: 0** 3° 5*** 6° 7 g*** Sample Input: 77777445521056 Sample Output2: 0* 1 2° 3 45 S. 6° 8 9 Sample Output 3: Sample Input3: 782222231567799988001332092555...

  • 2. Searching a String: Write a MIPS assembly language program to do the following: Read a...

    2. Searching a String: Write a MIPS assembly language program to do the following: Read a string and store it in memory. Limit the string length to 100 characters. Then, ask the user to enter a character. Search and count the number of occurrences of the character in the string. The search is not case sensitive. Lowercase and uppercase letters should be equal. Then ask the user to enter a string of two characters. Search and count the number of...

  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT