Create a C++ program that will read a series of titles from an input file (via L
ID: 3689018 • Letter: C
Question
Create a C++ program that will read a series of titles from an input file (via Linux redirection), one per line. Do not use fstream as input will be taken through cin.
The titles will be in an unorthodox mix of upper and lowercase letters and words may be separated by more than 1 blank.
No arrays or flags should be used in the program as well.
Reformat each title so that
-the first character (if it is a letter) of each word is capitalized and all remaining letters are lowercase
-each word is separated by exactly one blank space
-For example, "thE CAT in tHe hat" becomes "The Cat In The Hat".
REQUIREMENTS
At least one function (in addition to main) must be used.
The string data type must be used.
No global variables may be used.
The required output (displayed to the screen) from the program is
-a list of the reformatted titles, one per line
ASSUMPTIONS
-The input file will not be empty.
-There will be one title per line.
-Each title will consist of 1 or more words.
-There will be no leading spaces before the first word in the title.
-There will be at least 1 blank space between each word (there may be more).
-There will be a linefeed after the last character of the last word in a title (no trailing blanks).
-The program need only attempt to capitalize the first character of a word. For example, if a word in the title is "9TH", it should be reformatted to "9th". In other words, the program does not have to find the first letter in a word and capitalize it.
Sample terminal session:
[key]$ more data
the 5TH wAVE
the cAT in thE HAT
onE fish two FISh red fISh bLuE Fish
[key]$ g++ exercise.cpp
[key]$ ./a.out < data
The 5th Wave
The Cat In The Hat
One Fish Two Fish Red Fish Blue Fish
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
char character;
int size1,i;
string str,destinationpath;
string pathname;
cout<<"Please enter path of your file ;
cin>>pathname;
ifstream file(pathname);
string stringhere="",s3;
cout<<"Before ";
while (getline(file, s3))
{
cout<<s3;
stringhere=stringhere+s3;
}
cout<<" Out ";
size1=stringhere.size();
for (i=0; i<size1; i++)
{
if(i==0)
{
character=toupper(stringhere[i]);
stringhere[i]=character;
}
else if (isspace(stringhere[i]))
{
character=toupper(stringhere[i+1]);
stringhere[i+1]=character;
}
}
cout<<"now kindly enter your destination file name and path "
cout<<"(D:\personal data ewfile.txt): ";
cin>>destinationpath;
ofstream outputfile(destinationpath);
outputfile << stringhere << endl;
file.clear();
file.seekg(0, ios::beg);
system("pause");
return 0;
}