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

c++ problem: Use an STL stack to reverse a line of input characters that are read...
c++ problem: Use an STL stack to reverse a line of input characters that are read into a string. Your stack should contain the characters of the user's string. Use getline() for input – it needs to be part of your C++ tool inventory. A note on getline: Suppose I am doing the following - This program reverses a string using the STL stack Enter your string of less than 80 characters followed by an ENTER a string input Enter...
Write a C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
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 java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
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:"
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)
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT