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

In this lab you are to write a program to find all words used in a text. This pr

ID: 3710374 • Letter: I

Question

In this lab you are to write a program to find all words used in a text. This program will keep asking the user to type a text string. (This is in c++). For example, if the user inputs the following text:

”Have a good day. Have a good class. “+”Have a good visit. Have fun!”

You will do this program two different ways:

You will use a Set to contain the tokens obtained from the input text string, iterate over the contents of the Set and output the results.

You will use a UnorderedSet to contain the tokens obtained from the input text string, iterate over the contents of the UnorderedSet and output the results.

For example, the set of tokens of the above text string will be set<string> tokens={‘ “ ‘, “. “, “+”, “!” }

Or might be unorderedset contains the tokens.

Next you will write a program that counts the occurrences of words in a text (use the same text as before) and displays the words and their occurrences in ascending order of words. The program will use a Map to store a pair consisting of a word and its count. For each word, check whether it is already a key in the map. If not, add the key and value 1 to the map. Otherwise, increase the value for the word (key) by 1 in the map. Iterate over the Map to output the data in ascending order.

For example, the map of the above text string might look like the following:

have 4

a       3

good 3

day   1

class 1

visit 1

fun    1

your program can run multiple times, give the user to input a text string or quit the program.

Explanation / Answer

#include <bits/stdc++.h>

using namespace std;

int main() {

string str;

getline(cin,str); // To get input as the sentences not as the single string.

int i,n;

for(n=0;str[n]!='';n++);  // counting the length of sentence.

string str1="";  // taking an empty string.

map<string,int>mp; // declaring a map

map<string,int>::iterator it;  // declaring an iterator

i=0;

while(i<n){

if(str[i]==' '||str[i]==','||str[i]=='.'||str[i]=='!'){ // whenever we get these symbol then increase the count of word //by 1

mp[str1]++;

str1=""; // again make it as empty string to store another word.

}

else

str1=str1+str[i]; //concatenate the string and characters in order to make a word.

i++;}

for(it=mp.begin();it!=mp.end();it++)

cout<<it->first<<" "<<it->second<<endl; // it->first has word and it->second has that particular word //count(frequency)

return 0;

}

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