Question

In: Computer Science

Using C++, write a code that this program always stores text file output into a text...

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

Solutions

Expert Solution

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.


Related Solutions

C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
Write a C++ program using produces Huffman code for a string of text entered by the...
Write a C++ program using produces Huffman code for a string of text entered by the user. Must accept all ASCII characters.
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT