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
What are the major threats of using the internet? How do you use it? How do children use it? How canwe secure it? Provide four references with your answer. Two of the refernces can be from an article and the other two from websites.
Assume that a string of name & surname is saved in S. The alphabetical characters in S can be in lowercase and/or uppercase letters. Name and surname are assumed to be separated by a space character and the string ends with a full stop "." character. Write an assembly language program that will copy the name to NAME in lowercase and the surname to SNAME in uppercase letters. Assume that name and/or surname cannot exceed 20 characters. The program should be general and work with every possible string with name & surname. However, you can consider the data segment definition given below in your program. .DATA S DB 'Mahmoud Obaid." NAME DB 20 DUP(?) SNAME DB 20 DUP(?) Hint: Uppercase characters are ordered between 'A' (41H) and 'Z' (5AH) and lowercase characters are ordered between 'a' (61H) and 'z' (7AH) in the in the ASCII Code table. For lowercase letters, bit 5 (d5) of the ASCII code is 1 where for uppercase letters it is 0. For example, Letter 'h' Binary ASCII 01101000 68H 'H'…
What did you find most interesting or surprising about the scientist Lavoiser?

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