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

PLEASE TRY TO SOLVE THIS ONLY USING <strings>. LIBRARY. . Write a C++ program th

ID: 3853434 • Letter: P

Question

PLEASE TRY TO SOLVE THIS ONLY USING <strings>. LIBRARY.

. Write a C++ program that reads in and prints a text, line by line, and calls a series of
functions. The main program calls a function diffwords() to count the number of
different words in the entire text (ignoring case). It also calls a function wordcount() to
count the number of times each word appears in the text. Then it calls a function
printcount() to print a list of all the words in the text, together with the count of the
number of times they appear. For example, if a word occurs twice in the text, it appears
only once on the list, with a count of 2. Print the list of words in alphabetical order. Use
other functions wherever appropriate.

For example, suppose the text is this:
The elephant ate the banana and the giraffe ate the banana.
The function diffwords() produces a count of 6 ("the", "elephant", "ate", "banana", and
"giraffe"); wordcount() produces this list:
and 1
ate 2
banana 2
elephant 1
giraffe 1
the 4

Explanation / Answer

main.cpp


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;


int makearray(string[]);
int diffwords(int, string[],string[]);
int wordcount(string[], int, string);
void printcount(int[], string[], int);
void format(string&);
bool findword(int, string[], string);


const int MAX_WORDS = 120;
ofstream toFile("out.txt");
ifstream readFile("in.txt");

int main(){

string words[MAX_WORDS];
string uniqueWords[MAX_WORDS];

string text, temp;
int numOfWords = 0, numOfUniqueWords = 0, counter = 0;
bool lastWord = false;

//creates an array of the text with uniform formatted words(no punctuation)
//and all in lower case. this is used to compare the frequency of the words
//that appear uniquely to the whole text
readFile >> text;
do{
    format(text);
    words[numOfWords] = text;
    numOfWords++;
}while(readFile >> text);

/*
for(int i=0; i<numOfWords; i++)
    cout << "words["<< i << "] ->" << words[i] << endl;
*/
//from here on, another value containing the amount of elements is added
//numOfUniqueWords keeps track of how many unique words are in the uniform
//formatted text.
numOfUniqueWords = diffwords(numOfWords,words,uniqueWords);

int frequency[numOfUniqueWords];
for(int i=0; i <numOfUniqueWords; i++){
    temp = uniqueWords[i];
    counter = wordcount(words,numOfWords,temp);
    frequency[i] = counter;
}
printcount(frequency,uniqueWords,numOfUniqueWords);


//close streams and return 0
readFile.close();
toFile.close();
return 0;
}
void printcount(int frequency[], string uniqueWords[], int numOfUniqueWords){

toFile << left << setw(12) << "WORD";
toFile << "||";
toFile << setw(5) << "COUNT ";
toFile << "------------------------------ ";
for(int i=0; i<numOfUniqueWords; i++){
    toFile << left << setw(12) << uniqueWords[i];
    toFile << "||";
    toFile << setw(5) << frequency[i] << " ";
}
toFile << "------------------------------";
}
//wordcount returns the number of times a word appears in the text. it takes in
// an array for total words, an int for total elements in words, and the
//word that is to be found and counted.
int wordcount(string words[], int numOfWords, string countThis){
int uniqueCount = 0;
for(int j=0; j < numOfWords; j++){

    if(countThis == words[j]){
      uniqueCount++;
    }

}
return uniqueCount;
}//end wordcount

//returns the number of unique words. int num is the number of total words from the text
//words is the array of formatted words from the text, and uniqueWords is the array
//of strings to be filled with unique words. diffwords skips the first word because it is
//the first word and inherently unique. it compares the rest of the elements to everything
//earlier in the array. if it is not the first occurence of the word, it is not added to the
//uniqueWords array.
int diffwords(int num, string words[], string uniqueWords[]){
int currWord = 0, lastWord = num, count = 0;
string temp;
bool uniqueWordFlag;
for(currWord; currWord < lastWord; currWord++){
    temp = words[currWord];
    if(currWord == 0){
      uniqueWords[count] = temp;
      count++;
    }else{
      uniqueWordFlag = findword(currWord,words,temp);
      if(uniqueWordFlag){
        uniqueWords[count] = temp;
        count++;
      }else{
        //do noting;
      }
    }
}//end for loop
return count;
}//END diffwords

//findword returns a bool, false if word is found and true if the word is Unique.
//it takes in the current position of the the currWord counter to see where in
//the array words the diffwords function is. It looks backwards to see if the word
//has occured before. if it has, the word is not unique and it it continues to the
//next element in the words array. toFind is the word the diffwords is seeking
bool findword(int pos, string words[], string toFind){
for(int i=pos; i>0; i--){
    if(words[i-1] == toFind){
      return false;
    }
}
return true;
}//end findword

//format is a void function that takes in a string by reference and changes the string
//if it has any punctuation, periods, commas, or is upper case, it is made lower case
//this is to make sure that all the words read in and compared are the same case and
//can be processed as the same words
void format(string &s){
for(int i=0; i < s.length(); i++){
    if(isupper(s[i])){
      s[i] = tolower(s[i]);
    }
    if(ispunct(s[i])){
      s.erase(i,1);
    }
    if(s[i] == ','){
      s.erase(i,1);
    }
    if(s[i] == '.'){
      s.erase(i,1);
    }
}
}//end format


in.txt

Between the Buried and Me is an American progressive metal band from Raleigh, North Carolina.
Formed in 2000, the band consists of Tommy Giles Rogers, Jr. (lead vocals, keyboards), Paul Waggoner (guitars, backing vocals), Dustie Waring (guitars), Dan Briggs (bass, keyboards), and Blake Richardson (drums).
Their debut eponymous album was released through Lifeforce Records in 2002, shifting to Victory Records for subsequent releases until 2011.
Signing to Metal Blade in 2011, Between the Buried and Me released their first extended play, The Parallax: Hypersleep Dialogues in 2011 and its full-length follow-up The Parallax II: Future Sequence the following year.
Their eighth studio album, Coma Ecliptic, was released in 2015.

out.txt

WORD        ||COUNT
------------------------------
between     ||2  
the         ||6  
buried      ||2  
and         ||4  
me          ||2  
is          ||1  
an          ||1  
american    ||1  
progressive ||1  
metal       ||2  
band        ||2  
from        ||1  
raleigh     ||1  
north       ||1  
carolina    ||1  
formed      ||1  
in          ||5  
2000        ||1  
consists    ||1  
of          ||1  
tommy       ||1  
giles       ||1  
rogers      ||1  
jr          ||1  
lead        ||1  
vocals      ||2  
keyboards   ||2  
paul        ||1  
waggoner    ||1  
guitars     ||2  
backing     ||1  
dustie      ||1  
waring      ||1  
dan         ||1  
briggs      ||1  
bass        ||1  
blake       ||1  
richardson ||1  
drums       ||1  
their       ||3  
debut       ||1  
eponymous   ||1  
album       ||2  
was         ||2  
released    ||3  
through     ||1  
lifeforce   ||1  
records     ||2  
2002        ||1  
shifting    ||1  
to          ||2  
victory     ||1  
for         ||1  
subsequent ||1  
releases    ||1  
until       ||1  
2011        ||3  
signing     ||1  
blade       ||1  
first       ||1  
extended    ||1  
play        ||1  
parallax    ||2  
hypersleep ||1  
dialogues   ||1  
its         ||1  
fulllength ||1  
followup    ||1  
ii          ||1  
future      ||1  
sequence    ||1  
following   ||1  
year        ||1  
eighth      ||1  
studio      ||1  
coma        ||1  
ecliptic    ||1  
2015        ||1  
------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote