Question

In: Computer Science

Design and implement a C++ program read in a whole line of characters as the input...

Design and implement a C++ program

  • read in a whole line of characters as the input string;
  • count and display how many times
  • how frequently (among the letters) each (case insensitive) letter appears in the above mentioned input string;

Sample program execution:

An example of executing such a program is shown below. Note that the user input is in italic font.

Please enter a line of characters:
This is a really long line of characters!

There are 41 characters in the input line.
And among them, 33 are letters.

  letter  number   frequency
       A       4    0.121212
       B       0           0
       C       2   0.0606061
       D       0           0
       E       3   0.0909091
       F       1    0.030303
       G       1    0.030303
       H       2   0.0606061
       I       3   0.0909091
       J       0           0
       K       0           0
       L       4    0.121212
       M       0           0
       N       2   0.0606061
       O       2   0.0606061
       P       0           0
       Q       0           0
       R       3   0.0909091
       S       3   0.0909091
       T       2   0.0606061
       U       0           0
       V       0           0
       W       0           0
       X       0           0
       Y       1    0.030303
       Z       0           0

Solutions

Expert Solution

Code:-

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
   cout<<"Enter a string:";
   string str;
   getline(cin, str);
   int n = str.length();
   transform(str.begin(),str.end(),str.begin(),::toupper);
   char ch[n + 1];
   strcpy(ch, str.c_str());
   int letters=0;
   for(int j=0;j<n;j++)
   {
       if(ch[j]>=int('A')&&ch[j]<=int('Z'))
       {
           letters=letters+1;
       }
   }
   cout<<"There are "<<n<<" characters in the input line.\nAnd among them, "<<letters<<" are letters.";
   cout<<"\nletter number frequency"<<endl;
   for(int i=(int)'A';i<=int('Z');i++)
   {
       int count=0;
       char temp;
       for(int j=0;j<n;j++)
       {
           temp=char(i);
           if(ch[j]==temp)
           {
               count=count+1;
           }
       }
       float f=(double)count/letters;
       cout<<temp<<" "<<count<<" "<<f<<endl;
   }
   return 0;
}

Output:-

Please UPVOTE thank you...!!!


Related Solutions

Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked list. One way means that each node has only one pointer, Q, (not both a rear and a front pointer), so the program must use pointers-to-pointers. Include functions to insert(), remove(), for use in main() where the user is prompted "Enter "i" to insert a new element, "r" to remove an element, "q" to quit:"
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input a description of several process schedules (i.e., lists of send, receive or print operations). The output of your program will be a linearization of these events in the order actually performed, annotated with Lamport clock values. The input of the program will be a collection of processes, each with a list of operations to perform. The processes are named p1...pn for some n (you...
Implement a program in C++ that does the following: Ask the user and read at least...
Implement a program in C++ that does the following: Ask the user and read at least 10 numbers as input from the keyboard and stores them in a an array Displays the array of numbers on the screen (as is, before sorting) Sorts those numbers in the array using Selection Sort Algorithm Displays the array of numbers on the screen (AFTER sorting)
Program this using C please The program will read from standard input two things - a...
Program this using C please The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1 and string str2 such that any lower-case alphabet character (a-z) will...
1. Specification Write a C program to implement a simple calculator that accepts input in the...
1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...
Design and implement a C++ program that performs the following steps:Ask the user to enter a...
Design and implement a C++ program that performs the following steps:Ask the user to enter a positive integer number N; Your program may need to prompt the user to enter many times until it reads in a positive number;Let user to enter N (obtained in the previous step) floating point numbers, and count how many positive ones there are in the sequence and sum up these positive numbers; (Hint: negative numbers or 0 are ignored).Display result.You can and should use...
IN C++ PLEASE!!! Design and implement a program (name it Coins) that determines the values of...
IN C++ PLEASE!!! Design and implement a program (name it Coins) that determines the values of coins in a jar. The program prints out the total dollars and cents in the jar. The program prompts the user to enter the number of coins (quarters, dimes, nickels, and pennies). Print out the number of coins entered for each coin type on separate lines followed by the total amount of money in the jar as dollars and cents as shown below.
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);....
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);. Code a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered .The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Eg: Enter a sequence of characters: abAa1121X+&$%$dc[space] Distinct characters are: abA12X+&$%dc. Use C++
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT