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...
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.
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...
Which is FALSE about interneurons? Multiple Choice They sum excitatory and inhibitory synaptic inputs. They receive...
Which is FALSE about interneurons? Multiple Choice They sum excitatory and inhibitory synaptic inputs. They receive synaptic input from other neurons in the CNS. They deliver synaptic input on other neurons. They can transmit information between afferent neurons and efferent neurons. They make synapses on effector organs in the PNS.
Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string...
Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string that deletes each non-alphanumeric character in the original string. You should solve this problem in 2 ways, first use a for loop that iterates through each character in the string, the second should use a while loop that iterates through a range. Keep spaces in the new string also. Hint: You can invoke the isalnum() function on a character, and it will return True...
You can estimate the total number of text messages you send and receive in a year...
You can estimate the total number of text messages you send and receive in a year as follows: Count up the number of text messages sent and received during the one-hour period of a typical day when you do the most texting; then ... Multiply that count by 13, and multiply that result by 365, the number of days in a year. 13 is a guesstimate or SWAG for the ratio of the total number of messages sent and received...
Problem: Take 3 inputs string, integer, string. If incorrect number of lines are given then print...
Problem: Take 3 inputs string, integer, string. If incorrect number of lines are given then print an error message. Also have to make sure 2nd input is an integer and not a string by default. The function in the program should take the second string and insert it into the first string in the position indicated by the integer input given. The program cannot use strlen() or strncpy() functions and it should check the integer input to make sure it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT