Question
C++ please
Write a program that reads the following sentences from a file: I am Sam Sam I am That Sam I am That Sam I am I do not like that Sam I am Do you like green eggs and ham I do not like them But I do like spam! You will first have to create the text file containing the input, separate from your program. Your program should produce two output files: (i) (ii) one with the whole text in uppercase another one with statistics about the text a. the number of characters (non spaces) in the whole text b. the length of each line (using arrays) c. the length of the longest line d. a count of each time a letter appears in the file (s and S are the same letter only display letters if their count 0) (using vectors) the number of lines in the original file e. The content of both output files should also be displayed on screen Provide snippets showing all files: input, outputl, ouput2 and the screen
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

#include<string>

#include<fstream>

using namespace std;

char toUpper(char ch) { //convert a char to upper case

   if (ch >= 'a' && ch <= 'z')

       ch = ch - 32;

   return ch;

}

char toLower(char ch) { //conert a char to lower case

   if (ch >= 'A' && ch <= 'Z')

       ch = ch + 32;

   return ch;

}

int main() {

   string inFile;

   cout << "Enter input file name: ";

   cin >> inFile; //read file name

   ifstream in (inFile.c_str()); //open infile

   ofstream o1 ("Output1"); //open out files

   ofstream o2 ("Output2");

   ofstream o3 ("Output3");

   int charCount = 0;

   int lineCount = 0;

   int maxLineSize = 0;

   int currLineSize = 0;

   char ch;

   while (in >> noskipws >> ch) { //loop for each charecters

       o1 << toUpper(ch); //print lower to file 1

       o2 << toLower(ch); //print upper to file 2

       if (ch == '\n'){ //count line when new line feed is encounters

           lineCount ++;

           if (currLineSize > maxLineSize)

               maxLineSize = currLineSize;

           currLineSize = 0;

       }

       else { //for other char count chars

           charCount ++;

          currLineSize ++;

       }

   }

   o1.close();

   o2.close();

   o3 << "Total number of char: " << charCount << endl;

   o3 << "Total number of lines: " << lineCount << endl;

   o3 << "Max verse size: " << maxLineSize << endl;

   cout << "Total number of char: " << charCount << endl;

   cout << "Total number of lines: " << lineCount << endl;

   cout << "Max verse size: " << maxLineSize << endl;

   o3.close();

}

Add a comment
Know the answer?
Add Answer to:
C++ please Write a program that reads the following sentences from a file: I am Sam...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

    C Program In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters Your program must compile and run from the command line. The program executable must be named “vigenere” (all lower...

  • I am writing a program in C++, which requires me to read an input text file...

    I am writing a program in C++, which requires me to read an input text file using command line argument. However, I am using xcode on my Macbook to write C++ program, and use terminal instead of command. How do you use int main(int argc, char** argv[]) to read an input file. My professor requires us not to hard code the text file name like .open("example.txt"); Thank you!

  • Using C++ programming. Write a program that takes a string of input from the user and...

    Using C++ programming. Write a program that takes a string of input from the user and separates the string of words based on the premise that the string contains words whose first letter is uppercase. Then, display the phrase (or sentence) to a string in which the words are separated by spaces and only the first word of the phrase starts with an uppercase letter. For example, if the user enters "IAmTheTeacher", then the program would display: "I am the...

  • Hello, I am struggling with this C# WPF application for one of my classes. I am...

    Hello, I am struggling with this C# WPF application for one of my classes. I am using visual studio to work on it. The description for the problem is as follows: Create an app that can open a text file. Each line of the file contains an arbitrary number of words separated by spaces. Display the count for how many words are in the line and the count how many non-space characters are in the line on the form /...

  • please use c++ please write down the input file information please add comments for some codes...

    please use c++ please write down the input file information please add comments for some codes please do not use #include <bits/stdc++.h> we did not learn it yet thank you CSIT 575-Take Home Lab #11: Arrays To learn to code, compile and run a program processing characters. Assignment Plan and code a top-down modular program utilizing ARRAYS to solve the following problem, using at least 3 functions (called from the main() section or from another function) to solve the problem....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT