Write a C++ program that reads in and prints a text, line by line, and calls a s
ID: 3836589 • Letter: W
Question
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
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
vector<string> split(string str, char delimiter)
{
vector<string> internal;
stringstream ss(str); // Turn the string into a stream.
string tok;
while(getline(ss, tok, delimiter)) {
internal.push_back(tok);
}
return internal;
}
void wordCount(vector<string> words,vector<int> wordsCount){
for (int i=0;i<words.size();i++){
cout << wordsCount[i] << " " << words[i] << " ";
}
cout<< " " << endl;
}
void printAll(vector<string> words,vector<int> wordsCount){
for (int i=0;i<words.size();i++){
cout<< words[i]<<": "<<wordsCount[i]<<" ";
}
}
void diffwords(vector<string> list){
if(list.size()==0)
{
cout<<"the list is empty ";
return;
}else{
cout << list.size() << endl ;
vector<string> uniqueStrings;
vector<int> stringCount;
for(int i=0;i<list.size();i++)
{
bool duplicate=false;
for(int j=0;j<uniqueStrings.size();j++){
if(list[i]==uniqueStrings[j]){
//cout<<"match found! Add one to "<<uniqueStrings[j]<<" ";
stringCount[j]+=1;
duplicate=true;
break;
}
}
if (duplicate==false){
//cout<<"New word found! "<<list[i]<<" ";
uniqueStrings.push_back(list[i]);
stringCount.push_back(1);
}
}
cout <<" counts :" << uniqueStrings.size() << "( " ;
for (int i=0;i<uniqueStrings.size();i++){
cout<< uniqueStrings[i] <<",";
}
cout<< ")" << endl;
wordCount(uniqueStrings, stringCount);
printAll( uniqueStrings, stringCount);
}
}
int main(int argc, char **argv) {
string myCSV = "this is discovery channel and you are watching discovery channel wild life show.";
vector<string> sep = split(myCSV, ' ');
diffwords(sep);
}
description :
Added three function excluding main function.
1. diffwords
2. wordCount
3. printAll
please refer the below ouput
Output
-----------
13
counts :11( this,is,discovery,channel,and,you,are,watching,wild,life,show.,)
1 this 1 is 2 discovery 2 channel 1 and 1 you 1 are 1 watching 1 wild 1 life 1 show.
this: 1
is: 1
discovery: 2
channel: 2
and: 1
you: 1
are: 1
watching: 1
wild: 1
life: 1
show.: 1
Please let me knwo if you have face any chnallanges while reviewing hte code
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.