PROG+PROB. SOLV W/C++, ENH (SPEC VAL)
PROG+PROB. SOLV W/C++, ENH (SPEC VAL)
6th Edition
ISBN: 9781284564266
Author: Dale
Publisher: Jones & Barlett
bartleby

Concept explainers

Question
Book Icon
Chapter 7, Problem 1PSCS
Program Plan Intro

Program plan:

  1. Declare a variable file of ifstream type to read the input from the file.
  2. Declare a variable letter of type char to store the letter read from the file.
  3. Declare 6 variables upperCounter, lowerCounter, blank, digit, punctuation, allElseCounter of type int to store the counter of each type.
  4. Declare a variable inFile of type String to store the name of the file read from the user.
  5. A do- while loop is used to read letters from the file and to increment the each type of counter.
  6. A switch is used to check for each type of letters and to increment the counter inside do-while loop.

Program description:

The main purpose of the program is to summarize all the contents of the file in a table with details of each type of character scanned.

Expert Solution & Answer
Check Mark

Explanation of Solution

Program:

//inclusion of header files
#include <fstream> 
#include <iostream> 
#include <iomanip>
#include <cctype> 
//using namespace
using namespace std;
//main function
int main() 
{
ifstream file; //fstream input
char letter; 
// Declaration and Initialization of variables
int upperCounter = 0; // counter for uppercase letters
int lowerCounter = 0; // counter for lowercase letters
int blank = 0; // counter for blanks
int digit = 0; //counter for digits
int punctuation = 0; //counter for punctuation
int allElseCounter = 0; //counter for Remaining counters
//Declare variable to store filename
string inFile;
cout<<"Enter the filename to be processed" << endl;
cin>>inFile;
//open the file
file.open(inFile.c_str()); 
//if file doesn’t get opened
if (!file) 
{
    cout<< "Filename doesn’t exist." << endl;
    return 1; 
}
//else read the characters 
file.get(letter); // Input one letter 
do 
// process each letter
{
if (isupper(letter)) 
upperCounter++; 
else if (islower(letter)) 
lowerCounter++; 
else if (isdigit(letter)) 
digit++;
else 
switch (letter) 
{ 
case ' ' : blank++;
            break;
case '.' :
case '?' :
case '!' : punctuation++; 
            break; 
default : allElseCounter++; 
            break; 
} 
file.get(letter); 
} while (file);
// Calculate total 
float total = upperCounter + lowerCounter + blank + digit + punctuation + allElseCounter;
cout<<"Summary of letters: "<<  inFile << endl;
// Letters of each type
cout<<fixed<<setprecision(3)<<"Percentage of uppercase letters:"<<upperCounter / total* 100<<endl; 
cout<<fixed<<setprecision(3)<< "Percentage of lowercase letters:" << lowerCounter / total * 100 << endl; 
cout<<fixed<<setprecision(3)<< "Percentage of blanks: "<< blank / total * 100 << endl;
cout<<fixed<<setprecision(3)<< "Percentage of digits: "<< digit / total * 100 << endl; 
cout<<fixed<<setprecision(3)<< "Percentage of end-of-sentence: "<< "punctuation "<< punctuation / total * 100 << endl;
return 0;
}

Explanation:

In this program, first of all, the user is asked to enter the filename which needs to be processed. After entering the filename, all the characters of it are scanned to get a summary table of each type of letters. Each letter is scanned for uppercase letters, lowercase letters, for digits, blanks, and for any puctuations. Each type counter get incremented if the letter scanned is of that type. For example, if the letter scanned is of uppercase then the counter for uppercase letters will get incremented and so on. In the end, complete summary of all types of letters is printed on the screen with the filename which is scanned as shown in the output.

Output:

PROG+PROB. SOLV W/C++, ENH (SPEC VAL), Chapter 7, Problem 1PSCS , additional homework tip  1

Contents of the file Unclewill.txt:

PROG+PROB. SOLV W/C++, ENH (SPEC VAL), Chapter 7, Problem 1PSCS , additional homework tip  2

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
1. Create a Person.java file. Implement the public Person and Student classes in Person.java, including all the variables and methods in the UMLS. Person -name: String -street: String -city: String +Person(String name, String, street, String, city) +getName(): String +setName(String name): void +getStreet(): String +setStreet(String street): void +getCity(): String +setCity(String City): void +toString(): String Student -Id: int +Person(String name, String, street, String, city, int Id) +getId(): int +setId(int Id): void +toString(): String 2. Create a StudentTest.java file. Implement a public StudentTest class with a main method. In the main method, create one student object and print the object using System.out.println(). Your printing result must follow the example output: name: Mike, street: Morris Ave, city: Union, Id: 1000 Hint: You need to modify the toString methods in the Student class and Person class!
1) Apply the Paint Blue algorithm discussed in class to the following Finite Automata. a a a b b a COIS-3050H-R-W01-2025WI-COMB Formal Languages & Automata a b Show the status of the Finite Automata at the conclusion of the Paint Blue Algorithm (mark the visited states with an X and only include edges that have not been followed). 2) Use the pumping lemma to prove the following language is nonregular: L= {ab} = {abbb, aabbbbbb, aaabbbbbbbbb, ...}
3) Find CFGs that for these regular languages over the alphabet Σ= {a, b}. Draw a Finite Automata e CFG. 1 COIS-3050H-R-W01-2025WI-COMB Formal anguages & Automata Is that contain the substring aba. (b) The language of all words that have an odd number letters and contains the string bb. (c) The language of all words that begin with the substring ba and contains an odd number of letters. 4) Convert the following FA into a PDA. a a S± b a a Ν Ꮓ

Chapter 7 Solutions

PROG+PROB. SOLV W/C++, ENH (SPEC VAL)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning