Problem 1. Implement a C++ program that has the following functions: Reads in En
ID: 3668329 • Letter: P
Question
Problem 1. Implement a C++ program that has the following functions:
Reads in English text of an arbitrary length from the keyboard. Feel free to include this task in the main() function.
Identifies the least frequent letter in the above text. Implement a separate function getLeastFreqLetter() for this task. The main() function then calls this function to find out the least frequent letter and its frequency.
Calculate the frequency of each unique word (case insensitive). Implement another function countWordFreq() for this task. Words will be separated by spaces and punctuation. You can use the isspace and ispunct functions to detect separators (references here: http://www.cplusplus.com/reference/cctype/isspace/ and http://www.cplusplus.com/reference/cctype/ispunct/). Please define a global class that contains two public fields, a word and its count, for this task. You can then define a vector of this class to store all the unique words and their counts. The main() function will call this function to print out each word and its corresponding frequency.
Identify the most frequently occurring word in the paragraph. Implement another stand-alone function getMostFreqWord() for this purpose. This function will be called in main() to identify the most frequent word and its respective frequency.
Your program should prompt the person running it to enter text. Once they are done (once they press enter), your program should print out the information detailed above.
Explanation / Answer
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
/*to identify the least frequent letter in the above text*/
char getLeastFreqLetter(char* str,int &lCount)
{
// Create array to keep the count of individual
// characters and initialize the array as 0
int count[256] = {0};
// Construct character count array from the input
// string.
int len = strlen(str);
for (int i=0; i<len; i++)
count[str[i]]++;
int min = count[str[0]]; // Initialize min count
int pos=0; // to store result
// Traversing through the string and maintaining
// the count of each character
for (int i = 1; i < len; i++) {
if (count[str[i]]<min) {
min = count[str[i]];
pos=i;
}
}
lCount=min;
return str[pos];
}
int main()
{
/*Reads in English text of an arbitrary length from the keyboard*/
char input[100];
/*enter Words that will be separated by spaces and punctuation*/
cout<<endl<<"enter Words that will be separated by spaces and punctuation: "<<endl;
cin.getline(input, 100);
cout<<input;
int lCount=0;
/* getting least frequent character and its count*/
char c=getLeastFreqLetter(input,lCount);
cout<<endl<<"Least frequent letter: "<<c<<" It's frequency:"<<lCount;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.