In: Computer Science
Using C++, write a code that
this program always stores text file output into a text file named "clean.txt".
-The program should read one character at a time from "someNumbers.txt", and do the following.
-If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand.
-If it is a number, print that number to screen, but do NOT store it in the text file.
-If it is a space character, do one space in the screen AND in the text file as well.
-If it is a new line character, do a new line character on screen but two spaces in the text file instead.
-All other characters are simply ignored.
-The resulting text file should have no new line characters.
Output Screen:
Output:
hello2 world th5is
looks 5 quite strange
34dog
clean.txt:
hello world this looks quite strange dog
someNumbers.txt
hello2 World, th5is
looks !5 quite strange.
34dog
CODE -
#include<fstream>
#include<iostream>
using namespace std;
main()
{
ifstream infile;
ofstream outfile;
char ch;
// Opening files for reading and writing
infile.open("someNumbers.txt");
outfile.open("clean.txt");
// Iterating over each character in the file till end of file is reached
while(infile.get(ch))
{
// Converting the character read to lowercase character.
ch = tolower(ch);
// If the character is a letter or space character write it to the file and print it to the screen
if(isalpha(ch) || ch == ' ')
{
outfile << ch;
cout << ch;
}
// If the character is a number print it to the screen
else if(isdigit(ch))
cout << ch;
// If the character is a newline character, write two spaces in the file and print the newline character to the screen
else if(ch == '\n')
{
outfile << " ";
cout << ch;
}
}
// Closing the files.
infile.close();
outfile.close();
}
SCREENSHOTS -
INPUT FILE -
CODE & OUTPUT -
OUTPUT FILE -
If you have any doubt regarding the solution, then do
comment.
Do upvote.