Question

In: Computer Science

For this exercise, you will receive two string inputs, which are the names of the text...

For this exercise, you will receive two string inputs, which are the names of the text files. You need to check if the input file exists. If input file doesn't exist, print out "Input file does not exist."

You are required to create 4 functions:

void removeSpace (ifstream &inStream, ofstream &outStream): removes white space from a text file and write to a new file. If write is successful, print out "Text with no white space is successfully written to outFileName."

int countChar (ifstream &inStream): returns number of character in input file.

int countWord(ifstream &inStream): returns number of words in input file.

int countLines(ifstream &inStream): returns number of lines in input file.

After calling each function, you have to reset the state flags and move the stream pointer to beginning of file. You can do that by closing and re-opening the file, or by calling member functions:

inStream.clear(); inStream.seekg(0);

Example:

Input: input.txt output.txt

Output:

Text with no white space is successfully written to output.txt.

Number of characters is: 154

Number of words is: 36

Number of lines is: 10

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

void removeSpace(ifstream &inStream, ofstream &outStream);

int countChar(ifstream &inStream);

int countWord(ifstream &inStream);

int countLines(ifstream &inStream);

int main()

{

    string inp_fname, out_fname;

    cout << "Enter input filename: ";

    cin >> inp_fname;

    cout << "Enter output filename: ";

    cin >> out_fname;

    ifstream in;

    in.open(inp_fname.c_str());

    if (in.fail())

    {

        cout << "Input file does not exist." << endl;

        return -1;

    }

    ofstream out;

    out.open(out_fname.c_str());

    removeSpace(in, out);

    cout<<"Text with no white space is successfully written to "<<out_fname<<"."<<endl;

    cout<<"Number of characters is: "<<countChar(in)<<endl;

    cout<<"Number of words is: "<<countWord(in)<<endl;

    cout<<"Number of lines is: "<<countLines(in)<<endl;

    return 0;

}

void removeSpace(ifstream &inStream, ofstream &outStream){

    inStream.clear();

    inStream.seekg(0, ios::beg);

    char ch;

    while(inStream>>ch){

        outStream<<ch;

    }

}

int countChar(ifstream &inStream){

    inStream.clear();

    inStream.seekg(0, ios::beg);

    int count = 0;

    string line;

    while(!inStream.eof()){

        getline(inStream, line);

        count += line.size();

    }

    return count;

}

int countWord(ifstream &inStream){

    inStream.clear();

    inStream.seekg(0, ios::beg);

    int count = 0;

    string word;

    while(!inStream.eof()){

        inStream>>word;

        count++;

    }

    return count;

}

int countLines(ifstream &inStream){

    inStream.clear();

    inStream.seekg(0, ios::beg);

    int count = 0;

    string line;

    while(!inStream.eof()){

        getline(inStream, line);

        count++;

    }

    return count;

}


Related Solutions

15.1 File IO Warm-up For this exercise, you will receive two string inputs, which are the...
15.1 File IO Warm-up For this exercise, you will receive two string inputs, which are the names of the text files. You need to check if the input file exists. If input file doesn't exist, print out "Input file does not exist." You are required to create 4 functions: void removeSpace (ifstream &inStream, ofstream &outStream): removes white space from a text file and write to a new file. If write is successful, print out "Text with no white space is...
There are two text files, whose names are given by two String variables, file1 and file2....
There are two text files, whose names are given by two String variables, file1 and file2. These text files have the same number of lines. Write a sequence of statements that creates a new file whose name consists concatenating the names of the two text files (with a "-" in the middle) and whose contents of merging the lines of the two files. Thus, in the new file, the first line is the first line from the first file, the...
python program You are going to write a program that takes two inputs: A string representing...
python program You are going to write a program that takes two inputs: A string representing a list of names of the following form: first name 1, last name 1, (nick name 1); first name 2, last name 2, (nick name 2); ... A string representing part of or the complete family name. You are going to output all nick names for those names where there is a partial or complete match of the family name. If no match was...
Write a program called listful.py, in which the user inputs names, which get added to a...
Write a program called listful.py, in which the user inputs names, which get added to a list. Your program should: · Include a comment in the first line with your name. · Include comments describing each major section of code. · Create a list. · Ask the user to input a first name or hit enter to end the list. · If the user adds a first name (i.e., anything other than a blank value): o Add the name to...
In Java please. Prompt the user for two string inputs. Perform the following actions on these...
In Java please. Prompt the user for two string inputs. Perform the following actions on these inputs, and print the result of each action: Part I: Concatenate the two strings into one. Compare the two strings and check whether they are the same (ignore the case). Look up the method equalsIgnoreCase() from Java API Convert all the characters in a string to uppercase Look up the method toUpperCase() from Java API Part II: Replace all the 'd' characters with ‘e'...
In this Exercise, you have to take a single string as input. Using this input string,...
In this Exercise, you have to take a single string as input. Using this input string, you have to create multiple queues in which each queue will comprise of separate word appeared in input string. At the end, you will again concatenate all queues to a single queue.s Example: String = “Data Structure and Algo” Q1 = D → a → t → a Q2 = S → t → r → u → c → t → u →...
Write a Python program, phone.py, that inputs a string of characters which represent a vanity telephone...
Write a Python program, phone.py, that inputs a string of characters which represent a vanity telephone number, e.g., 800-MYPYTHON, and prints the all numeric equivalent, 800-69798466. You should implement this using a loop construct to process each character from left to right. Build a new string that is the all numeric equivalent and then print the string.
Utilizing PHP and HTML code Create a form that has text inputs for two numbers and...
Utilizing PHP and HTML code Create a form that has text inputs for two numbers and a submit button. Submit the values to a program that calculates and displays the GCD of the two numbers. The greatest common divisor of two integers a and b is the largest integer that is a factor of both a and b. The GCD of any number and 1 is 1, and the GCD of any number and 0 is that number. For example:...
IN JAVA WITH COMMENTS, The assignment: This program inputs the names of 5 students and the...
IN JAVA WITH COMMENTS, The assignment: This program inputs the names of 5 students and the (integer) grades they earned in 3 tests. It then outputs the average grade for each student. It also outputs the highest grade a student earned in each test, and the average of all grades in each test. Your output should look like this: Mary's average grade was 78.8% Harry's average grade was 67.7% etc... : In test 1 the highest grade was 93% and...
Write a program that inputs a string that represents a binary number. The string can contain...
Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements. If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains. If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console. Examples of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT