In: Computer Science
Design and implement a C++ program
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
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...!!!