In: Computer Science
PLEASE do the following problem in C++ with the screenshot of the output. THANKS!
Assuming that a text file named FIRST.TXT contains some text written into it, write a class and a method named vowelwords(), that reads the file FIRST.TXT and creates a new file named SECOND.TXT, to contain only those words from the file FIRST.TXT which start with a lowercase vowel (i.e., with 'a', 'e', 'i', 'o', 'u'). For example, if the file FIRST.TXT contains Carry umbrella and overcoat when it rains Then the file SECOND.TXT shall contain umbrella and overcoat it.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#include<iostream>
#include<fstream>
using namespace std;
//required class containing methods
//change the class name as you like
class VowelsFinder{
//variables to store names of input and output file
string input, output;
public:
//constructor
VowelsFinder(){
//initializing file names
input="FIRST.TXT";
output="SECOND.TXT";
}
//method to extract words from input file, write to output file all words that start with
//lower case vowel.
void vowelwords(){
//opening input file
ifstream inFile(input.c_str());
//checking if file opened correctly, displaying error and exiting if not opened
if(!inFile){
cout<<"cannot find "<<input<<endl;
return;
}
//doing the same for output file
ofstream outFile(output.c_str());
if(!outFile){
cout<<"cannot create "<<output<<endl;
return;
}
string word;
//looping and reading each word from input file one by one. assuming words are
//separated by space or tabs
while(inFile>>word){
//finding first letter
char c=word[0];
//checking if it is a lower case vowel
if(c=='a' || c=='e'|| c=='i' || c=='o' || c=='u'){
//writing to output file, separated by space
outFile<<word<<" ";
}
}
//closing both files
inFile.close();
outFile.close();
}
};
int main(){
//creating an object of the above class
VowelsFinder vowelsFinder;
//calling vowelwords. if everything works correctly, the system will not display any
//output, but creates the output file as needed.
//make sure you have FIRST.TXT file in same directory.
vowelsFinder.vowelwords();
return 0;
}
/*FIRST.TXT file screenshot*/
/*SECOND.TXT file screenshot (after running the program)*/