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:...
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...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string you entered is: aBC_Three_DEf An output for just this specific input will be fine. If you want to provide a program for all outputs, it would help with my understanding of how the program works overall as well but it is not needed. Thanks!
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...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
(Use the string class to solve the problem) Write a program (in c++) that can be...
(Use the string class to solve the problem) Write a program (in c++) that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or him”. Be sure to preserve...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT