Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Reads in English text of an arbitrary length from the keyboard. Feel free to inc

ID: 3669081 • Letter: R

Question

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. 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 <string>
#include <vector>
using namespace std;

struct frequentWords{
    string word;
    int freq;
};

/**Precondition: A string and a reference of an int array, the array corresponding to the 26 letters of the alphabet.
*Postcondition: a vector of struct, frequentWords, consisting of the letter and its frequency
*@param The string inputted by the user from main is passed to this function
*@return Returns a vector struct consisiting of the letter and the frequency. Using the struct in this function so that
           I can return back to the main both letter and frequency. Using vector in case of equal least frequent letters.
*/

vector<frequentWords> getLeastFreqLetter(string &s, int (&countLetter)[26]){
    vector<frequentWords> leastLetter(26);
   char dummyLetter = 'a';
   int dummyFreq = 10000;
   int index = 0; //This is needed to prevent a weird glitch from having if I had k instead of index. Same goes for the other method.
    for(int k=0; k<26;k++){
        countLetter[k] = 0;
   }
    //Counts frequency
   for(int i=0; i<s.length(); i++){
        int index = s[i]-'a';
        if(index>=0 && index<=25){
          countLetter[index]++;
       }
   }
   //Determines least frequency
   for(int j=0; j<26; j++){
       if(countLetter[j]<dummyFreq && countLetter[j]!=0){
          dummyFreq = countLetter[j];
      }
    }
    //Determines multiple frequent letters.
    for(int k = 0; k<26; k++){
        if(countLetter[k] == dummyFreq){
            leastLetter[index].freq = countLetter[k];
           dummyLetter = k + 'a';
           leastLetter[index].word = dummyLetter;
           index++;
       }
   }
   leastLetter.resize(index);
   
   return leastLetter;
}

/**Precondition: Parameters are the string s and a reference of the struct/frequentWords array
*Postcondition: Returns an integer

*@return I'm returning an integer so that I won't have to resize the vector when it prints out the list of words and frequencies.
           I had some trouble using the resize() function when concerning vectors of structs. So I'm avoiding that.
*/

int countWordFreq(string &s, vector <frequentWords> &countContainer){
    string dummy[100];
    int index = 0;
    int len = s.length();
   string temp;
  
   /** Using the following delimiters, I'm making it so that if it sees those delimiters, then
      * it'll know that a word has been created. The logic is, usually whitespace and punctuation are followed
      * after a word. I probably didn't need to call the erase function, but I decided to anyway.
      * The if statement where if it'll see the next punctuation takes care of double white spaces
      * and punctuation. But no more than that, for example "Mary had a   little lamb".
      * Also, each paragraph inputted must end in a period, or else that word will not count.
      * I noticed that when I put a colon ':' as a delimiter, the program doesn't output correctly. I wasn't
      * sure how to treat it, so I left it out, also because the prompt didn't specifically say colons too.
   */
   for(int i = 0; i<len;i++){
       if(s[0] == '.' || s[0] == ',' || s[0] == ';' || s[0] == '?' || s[0] == ' '){
               dummy[index] = temp;
              index++;
              s.erase(0, 1);
              temp.clear();
              if(s[0] == ' ' || s[0] == '.' || s[0] == ',' || s[0] == ';' || s[0] == '?'){
                  s.erase(0, 1);
                  len--;
           }
       }
       else{
          temp += s[0];
          s.erase(0, 1);
       }
   }
   /** The next set of lines will take care of duplicates and counting the frequency.
       * Ended up reusing temp and index because I didn't want to create new variables and take up
       * more space in memory.
   */
   index = 0;
  
   for(int j = 0; j<100; j++){
       temp = dummy[j];
       bool truthtest = false;
       for(int k=0;k<100;k++){
           if(temp == countContainer[k].word){
              countContainer[k].freq++;
              truthtest = true;
              break;
           }
       }
       if(truthtest == true){
          continue;
       }
       else{
          countContainer[index].word = temp;
          countContainer[index].freq++;
          index++;
       }
   }

   return index;
  
}

/**Precondition: Parameter is the reference of the vector wordContain and int loop.
*Postcondition: returns a vector struct of frequentWords
*@param I made the parameter a const because it's read only, and a reference to not increase memory usage.
          I also made the int loops a parameter because I found that if I didn't do it, it would include white space as a
          word.
*@return I returned a vector struct of frequent words because there might be multiple most frequent words.
*/

vector<frequentWords> getMostFreqWord(const vector<frequentWords> &wordContain, int loops){
   vector<frequentWords> mostWords(loops);
   int index = 0;
    int mostFrequency = 0;
    string wordDummy;
   //Determines most frequency.
   for(int i = 0; i<loops;i++){
       if(wordContain[i].freq>mostFrequency){
           mostFrequency = wordContain[i].freq;
       }
   }
   //Determines multiple most frequent words.
   for(int j = 0;j<loops; j++){
        if(wordContain[j].freq == mostFrequency){
            mostWords[index].word = wordContain[j].word;
           mostWords[index].freq = mostFrequency;
           index++;  
       }
   }
  
   mostWords.resize(index);
   return mostWords;
}

int main(){
   
    string paragraph;
    int letterContainer[26];
    vector<frequentWords> countWords(100);
   
    cout << "Please enter your paragraph of at least 100 words here, ending with a period:" <<endl;
    getline(cin, paragraph);
  
   //This for loop makes every letter lower case.
   for(int i = 0; i<paragraph.length(); i++){
       if(paragraph[i]>=65 && paragraph[i]<=90){
           int temp = paragraph[i]-'A';
           paragraph[i] = (char) (temp+'a');
       }
   }
   
   vector<frequentWords> lstFrqLttr = getLeastFreqLetter(paragraph, letterContainer);
   cout<<endl;
   cout<< "The least frequent letter(s) and its frequency are: " <<endl;

   for(int k = 0; k<lstFrqLttr.size(); k++){
       cout<<"'"<<lstFrqLttr[k].word<<"' | "<<lstFrqLttr[k].freq<<endl;
   }

   int numOfLoops = countWordFreq(paragraph, countWords);
   cout<<endl;
   cout<< "This is the list of words and their frequency: "<<endl;
   for(int j = 0; j<numOfLoops; j++){
       cout<< countWords[j].word << ": " << countWords[j].freq<<endl;
   }
  
   vector<frequentWords> mstFrqWrd = getMostFreqWord(countWords, numOfLoops);
   cout<<endl;
   cout<< "The most frequent word(s) and its frequency are: " <<endl;
   for(int l = 0; l<mstFrqWrd.size(); l++){
       cout<<"'"<<mstFrqWrd[l].word<<"' | "<<mstFrqWrd[l].freq<<endl;
   }
    return 0;
}


output

                                                                                                                                                  
Please enter your paragraph of at least 100 words here, ending with a period:                                                                               
if you can imagine it you can achieve it if you can dream it you can become it.                                                                             
                                                                                                                                                            
The least frequent letter(s) and its frequency are:                                                                                                         
'b' | 1                                                                                                                                                     
'd' | 1                                                                                                                                                     
'g' | 1                                                                                                                                                     
'h' | 1                                                                                                                                                     
'r' | 1                                                                                                                                                     
'v' | 1                                                                                                                                                     
                                                                                                                                                            
This is the list of words and their frequency:                                                                                                              
if: 2                                                                                                                                                       
you: 4                                                                                                                                                      

This is the list of words and their frequency:                                                                                                              
if: 2                                                                                                                                                       
you: 4                                                                                                                                                      
can: 4                                                                                                                                                      
imagine: 1                                                                                                                                                  
it: 4                                                                                                                                                       
achieve: 1                                                                                                                                                  
dream: 1                                                                                                                                                    
become: 1                                                                                                                                                   
                                                                                                                                                            
The most frequent word(s) and its frequency are:                                                                                                            
'you' | 4                                                                                                                                                   
'can' | 4                                                                                                                                                   
'it' | 4                                                                                                                                                    
sh-4.3$