In: Computer Science
Write a program that reads from the external file input.txt, capitalizes all words that begin with the letter "a," and then writes them to an external file output.txt (Note: Do not forget to copy the blanks. You may wish to use infile.get and outfile.put in your program.) Output: After the program generates output.txt, the code should display the contents of the file on the screen to verification.
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.
Thank You!
===========================================================================
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
int main(){
char input_filename[] ="input.txt";
char output_filename[] ="output.txt";
ifstream infile(input_filename);
ofstream outfile(output_filename);
if(infile.bad() || outfile.bad() || !infile.is_open()
|| !outfile.is_open()){
cout<<"Error: File(s) could
not be accessed or opened.\n";
return 1;
}
char currChar;
char prevChar;
int count = 0;
while(infile.get(currChar)){
if(currChar=='a' &&
(count++==0 || prevChar==' ')){
outfile.put('A');
}
else{
outfile.put(currChar);
}
prevChar = currChar;
}
infile.close();
outfile.close();
cout<<"Output file:
"<<output_filename<<" generated successfully.\n";
return 0;
}
===============================================================