Question

In: Computer Science

use C++ Write a program to calculate the frequency of every letter in an input string...

use C++

  1. Write a program to calculate the frequency of every letter in an input string s.
    1. Your program should be able to input a string.
    2. Calculate the frequency of each element in the string and store the results
    3. Your program should be able to print each letter and its respective frequency.
    4. Identify the letter with the highest frequency in your message.
    5. Your message should be constructed from at least 50 letters.

Example of Output:

letters

frequency

a

10

b

4

c

6

v

12

s

20

z

1

r

3

Letter s has the highest frequency of 20.

Solutions

Expert Solution

Code is

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
int i, len;

// declare a array to store frequency of each character
int freq[26];

/* Input string from user */
cout<<"Enter any string: (atleast 50 characters)";
getline(cin,s);

len = s.size();
/* Initialize frequency of each character to 0 */
for(i=0; i<26; i++)
{
freq[i] = 0;
}

/* Find total number of occurrences of each character */
for(i=0; i<len; i++)
{

/* If the current character is lowercase alphabet */
if(s[i]>='a' && s[i]<='z')
{
freq[(int)s[i] - 97]++;
}

// else upper case
else if(s[i]>='A' && s[i]<='Z')
{
freq[(int)s[i] - 65]++;
}
}

// To get max frequent letter use a loop
int max=freq[0],maxposition;
for(int i=1;i<26;i++)
{
if(freq[i]>max)
{
max = freq[i];
maxposition=i;
}
}

/* Print the frequency of all characters in the string */
cout<<"\nFrequency of all characters in the given string: \n";
cout<<"Letter \t frequency\n";
for(i=0; i<26; i++)
{

/* If current character exists in given string */
if(freq[i] != 0)
{
cout<<(char)(i + 97)<<" \t"<< freq[i]<<endl;
}
}

cout<<"Letter "<<(char)(maxposition+97)<<" has the highest frequency of "<<max;
return 0;
}

Code Screen Shot

Output

This is how you can write a program in C++ to get frequency of each characters in string s

If u like the answer then Give It a Thumbs Up and have any doubt comment it


Related Solutions

Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Write a basic C++ program with function, whose input is a character and a string, and...
Write a basic C++ program with function, whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n...
In objective-C Task: Write a program whose input is a character and a string, and whose...
In objective-C Task: Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: n Monday the output is:...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Q20. Using C++ style string to write a program that reads a sentence as input and...
Q20. Using C++ style string to write a program that reads a sentence as input and converts each word of the sentence following the rule below: For every word in the sentence, the first letter is relocated the end of the word. Then append the string “KPU” to the word. More requirements: All letters in the output should be uppercase. More assumptions: The input sentence contains no non-alphabetic letters Sample Run: Please enter the original sentence: i LOVE to program...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Create program which verifies if input string is a valid variable declaration or not. Use C...
Create program which verifies if input string is a valid variable declaration or not. Use C programming language. - This program can only use the following variable types: char, float, and int - Remove any newline \n from input string - The input prompt should say ">>> " - If input declaration is valid, it should print "Valid dec\n" - If input declaration is invalid, it should print "Invalid dec\n" - Make sure the identifier entered matches the rules of...
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT