Count how many words start with each letter Also make a set
Count how many words start with each letter. Also make a set of result files that store all words starting with each letter. C++ Problem.
Task 1 Review the ASCIl character codes for this lab ASClIl Table Read about how files work in C++ https://www.tutorialspoint.com/cplusplus/cpp files streams.htm Task 2 Create a new file lab6.cpp Write a function int starts_with letter(string filename,char letter) that takes a file and writes all the words that start with the given letter into a file. The function returns the number of words it put into the file. Here is an outline for how your function should work 1. Open the filename using an ifstream and check to make sure it worked. 2. Create a new file to save the results in using an ofstream. The file should be named based on the letter given. For example, if I call start with letter(\"example.txt\",\'A) then the output file should be results_A.txt. 3. Read each word in the file. If it starts with the upper or lower case version of the letter given as input, write the word to your output file followed by a newline and increase your count 4. Close both files and return the count. Hints: Use ascii codes to account for both upper and lower case letters. The below code shows you how to generate a file name to use. string res-name=\"results-\"; res_name+letter; res_name+\"txt Task 3 Write your main program. Ask the user for the name of a file. Open the file and count how many words starting with each letter A-Z appear. The output is the total number of words that start with each letter. The program should also make a file for each letter with the actual words found, one word per line. When the program exists, you should have 26 new files with results. (See Attached Example File) Task 4 Go to Project Gutenberg and download a plain text copy of \"Fundamental Principles of the Metaphysic of Morals\" by Immanuel Kant. Run your program on it. Books from Project Gutenberg have copyright and additional material at the beginning and end, just leave this in the file as it is for this lab.Solution
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <conio.h>
using namespace std;
int main()
{
deque <string> text; // this is the text of the file we will modify
ifstream in_stream; //declaring the file input
string filein, search, str, replace; //declaring strings
int lines = 0, characters = 0, words = 0; //declaring integers
char ch;
cout << \"Enter the name of the file\ \"; //Tells user to input a file name
cin >> filein; //User inputs incoming file name
in_stream.open (filein.c_str(), ios::in | ios::binary); //Opens the file
//FIND WORDS
cout << \"Enter word to search (and replace): \" <<endl;
cin >> search; //User inputs word they want to search
//REPLACE WORDS
cout << \"\ Enter the word you want to replace it with: \" <<endl;
cin >> replace;
while (!in_stream.eof())
{
getline(in_stream, str);
lines++;
// if I need print the original it goes here...
string::size_type nIndex = str.find(search);
while(nIndex != string::npos)
{
cout << \"found in Line \" << lines << \" at Index \" << nIndex;
// if nIndex is valid we have the start position of the word we want to replace.
// cut the piece out of the string and insert the new piece
// we should have the lengths of the search and the replace to make it easy.
nIndex = str.find(search);
}
// we don\'t care if we change the line or not at this point....
// If I just need to print the modified line display here....
text.push_back (str);
// count the characters.
string::iterator pos;
for(str.begin(); str.end(); pos++)
{
characters++;
if(*pos == \' \') // find a space...
words++;
}
}
// if I need to write a new file .. I have the stuff buffered, dump the buffer.
system(\"PAUSE\");
return EXIT_SUCCESS;
}

