Question

C++, program that use the built in string class, its constructors, some of its methods and...

C++, program that use the built in string class, its constructors, some of its methods and operators. It reads an input file of English text, one word at a time. The file should consist of about 500 words. Keep only the words, removing any punctuation or other characters. .Store the words in a built-in STL container, such as vector or map.,Choose a list of about 10 words, for example, {she,, and, he, or, the}., Remove these words from the list.,, Next count the number of occurrences of the 3 most frequent words. • Implement the code with optimized algorithms.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// aboutComputer.txt (Input file)

A computer is a device that can be instructed to carry out an
arbitrary set of arithmetic or logical operations automatically.
The ability of computers to follow a sequence of operations
called a program make CAT computers very flexible and useful.
Since ancient times simple CAT manual devices like the abacus
aided people in doing calculations. Early in the Industrial
Revolution, some mechanical devices were built to automate
long tedious tasks, such as guiding patterns for looms.

=========================================

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

using namespace std;
void addWord(map<std::string,int> &words,string s);
void readFile(string infile,map<std::string,int> &words);
void display(map<std::string,int> words);
  
int main() {
// Declaring variables
std::map<std::string,int> words;
  
  
//defines an input stream for the data file
ifstream dataIn;

  
string infile;
cout<<"Please enter a File Name :";
cin>>infile;
readFile(infile,words);
  
display(words);

  
return 0;
}
void readFile(string infile,map<std::string,int> &words)
{
string s;
//defines an input stream for the data file
ifstream dataIn;
//Opening the input file
dataIn.open(infile.c_str());
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"'"<<infile<<"' File Not Found **";
exit(0);
}
else
{
//counting how many words in the file
while(dataIn>>s)
{
addWord(words,s);
}
dataIn.close();
}
}
void addWord(map<std::string,int> &words,string s)
{
char c;
string str="";
for(unsigned int i=0;i<s.length();i++)
{
c=s[i];
if(isalpha(c))
str.push_back(std::toupper(c));
}
++words[str];
}
void display(map<std::string,int> words)
{

cout<<"---------------------"<<endl;
cout<<"Word Counter Summary "<<endl;
cout<<"---------------------"<<endl;
cout<<"\nWord\t\tCount"<<endl;
cout<<"------\t\t-----"<<endl;
  
for(std::map<std::string,int>::iterator iter = words.begin(); iter!=words.end(); ++iter)
{
cout<<setw(16)<<left<<iter->first<<setw(5)<<right<<iter->second<<std::endl;
}
  
}

=======================================

Output:

C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\ReadFileWordFrequencyMap.exe Please enter a File Name :aboutComputer.txt Word Coun

OUT PATTERNS PEOPLE PROGRAM REVOLUTION SEQUENCE SET SIMPLE SINCE SOME SUCH TASKS TEDIOUS THAT THE TIMES TO USEFUL VERY WERE 1

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
C++, program that use the built in string class, its constructors, some of its methods and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 1. Implement a C++ program that has the following functions: Reads in a paragraph of...

    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...

  • Write a program, called wordcount.c, that reads one word at a time from the standard input....

    Write a program, called wordcount.c, that reads one word at a time from the standard input. It keeps track of the words read and the number of occurrences of each word. When it encounters the end of input, it prints the most frequently occurring word and its count. The following screenshot shows the program in action: adminuser@adminuser-VirtualBox~/Desktop/HW8 $ wordCount This is a sample. Is is most frequent, although punctuation and capitals are treated as part of the word. this is...

  • Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that...

    Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...

  • Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Implement the histogram function to complete the desired program. You must use dynamically allocated arrays for...

    Implement the histogram function to complete the desired program. You must use dynamically allocated arrays for this purpose. For your initial implementation, use ordered insertion to keep the words in order and ordered sequential search when looking for words. Note that the array utility functions from the lecture notes are available to you as art of the provided code. Although we are counting words in this program, the general pattern of counting occurrences of things is a common analysis step...

  • Recursion and Trees Application – Building a Word Index Make sure you have read and understood...

    Recursion and Trees Application – Building a Word Index Make sure you have read and understood ·         lesson modules week 10 and 11 ·         chapters 9 and 10 of our text ·         module - Lab Homework Requirements before submitting this assignment. Hand in only one program, please. Background: In many applications, the composition of a collection of data items changes over time. Not only are new data items added and existing ones removed, but data items may be duplicated. A list data structure...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • please write a C++ code Problem A: Word Shadow Source file: shadow.cpp f or java, or.cj...

    please write a C++ code Problem A: Word Shadow Source file: shadow.cpp f or java, or.cj Input file: shadow.in in reality, when we read an English word we normally do not read every single letter of that word but rather the word's "shadow" recalls its pronunciation and meaning from our brain. The word's shadow consists of the same number of letters that compose the actual word with first and last letters (of the word) in their original positions while the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT