This assignment is an exercise in reading and writing files, manipulating string
ID: 3674777 • Letter: T
Question
This assignment is an exercise in reading and writing files, manipulating strings and string functions, gathering statistics, and the use of complex functions. Read in an input file name. If it is not a valid file continue to prompt the user until a valid file is read in. Read in an output file name. If it exists it will be over written. All output should go to the output file instead of standard output. Write the input file to the output file and make all alphabetic characters upper case. Count the number of characters. Count the number of words (words are groups of one or more letters separated by a space, tab, return character or punctuation). Keep track of the size of the largest word. The size of the word should ignore any punctuation. Keep track of the size of the smallest word. The size of the word should ignore any punctuation. Print out the number of characters, number of words, size of the smallest word, size of the largest word. The file will contain only words (no numbers just minor punctuation). There will be no limit to the number of words. You must open the file and read each individual word using standard input (cin) until the end-of-file is detected calling The files must be closed properly. No global variables, other than constants! Use the const qualifier on member functions wherever it is appropriate. You will need to use the library for output You must use functions with parameters and return values.Explanation / Answer
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
int getSmallestWordLengthInFile(vector<int> myLengthvector);
int getLargestWordLengthInFile(vector<int> myLengthvector);
int main () {
string inFile,outFile;
ofstream fout;
cout << "Enter the input filename: ";
cin >> inFile;
cout << "Enter the output filename: ";
cin >> outFile;
ifstream fin;
char c;
char upperChar;
fin.open(inFile, ios::in);
fout.open (outFile);
std::vector<int> myLengthvector;
int charCount=0;
int wordCount=0;
int length=0;
while (!fin.eof() ) {
fin.get(c);
if (c != ' ')
{
upperChar=std::toupper(c);
fout.put(upperChar);
charCount++;
length++;
}
else if(c==' ' || c==' '){
wordCount++;
fout.put(c);
myLengthvector.push_back(length);
length=0;
}
}
cout << "number of words in file " << wordCount;
cout << "number of chars in file " << charCount;
cout << "length of larget word in file " << getLargestWordLengthInFile(myLengthvector);
cout << "length of smallest word in file " << getSmallestWordLengthInFile(myLengthvector);
fin.close();
fout.close();
return 0;
}
int getSmallestWordLengthInFile(vector<int> myLengthvector){
std::sort (myLengthvector.begin(), myLengthvector.end());
return myLengthvector.front();
}
int getLargestWordLengthInFile(vector<int> myLengthvector){
std::sort (myLengthvector.begin(), myLengthvector.end());
return myLengthvector.back();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.