Problem 1. Implement a C++ program that has the following functions: Reads in a
ID: 640771 • Letter: P
Question
Problem 1. Implement a C++ program that has the following functions:
Reads in a paragraph of English text up to 100 words from the keyboard and stores this paragraph in a string object. Feel free to include this task in the main() function.
Identifies the least frequent letter (case insensitive) in the above paragraph. 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. Use white spaces and punctuation marks such as comma, period, semicolon and question mark as word delimiters. Please define a global struct that contains two fields, word and its count for this task. You can then define an array of this struct to store all the unique words and its 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.
Explanation / Answer
#include <iostream>
using namespace std;
struct info{
string word;
int freq;
};
void getLeastFreqLetter(string arr){
int data[27] = {0};
for(int i = 0; i < arr.size(); ++i){
if(arr[i] >= 65 && arr[i] <= 90){
arr[i] += arr[i] + 32;
}
if(arr[i] >= 97 && arr[i] <= 122){
data[arr[i] - 97]++;
}
}
int minind = 0;
for(int i = 1; i < 26; ++i){
if(data[minind] > data[i]){
minind = i;
}
}
cout << "Least Frequency Character is " << (char)(minind + 97) << " and frequency is " << data[minind] << endl;
}
int isPresent(info *arr, string temp, int count){
for(int i = 0; i < count; ++i){
if(arr[i].word == temp){
return i;
}
}
return -1;
}
info *countWordFreq(string line, int &count){
info *arr = new info[100];
count = 0;
string temp = "";
int flag = 1;
for(int i = 0; i < line.size(); ++i){
while(line[i] == ' ' || line[i] == ' ' || line[i] == ',' || line[i] == '.' || line[i] == ';'){
flag = 0;
++i;
}
if(flag == 1){
temp += line[i];
}
else{
int ind = isPresent(arr, temp, count);
if(ind != -1){
arr[ind].freq += 1;
}
else{
arr[count].word = temp;
arr[count].freq = 1;
count++;
}
temp = "";
temp += line[i];
flag = 1;
}
}
if(temp != ""){
int ind = isPresent(arr, temp, count);
if(ind != -1){
arr[ind].freq += 1;
}
else{
arr[count].word = temp;
arr[count].freq = 1;
count++;
}
}
for(int i = 0; i < count; ++i){
cout << arr[i].word << "( " << arr[i].freq << " )" << endl;
}
return arr;
}
info getMostFreqWord(info *arr, int count){
int maxind = 0;
for(int i = 1; i < count; ++i){
if(arr[maxind].freq < arr[i].freq){
maxind = i;
}
}
return arr[maxind];
}
int main(){
string line;
getline(cin, line);
getLeastFreqLetter(line);
int count = 0;
info *arr = countWordFreq(line, count);
info most = getMostFreqWord(arr, count);
cout << "Most Frequent word is " << most.word << endl;
cout << "It's frequency is " << most.freq << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.