Programming and Problem Solving With C++
Programming and Problem Solving With C++
6th Edition
ISBN: 9781449694265
Author: Nell Dale
Publisher: Jones & Bartlett Learning
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:

Programming and Problem Solving With C++, Chapter 7, Problem 1PSCS , additional homework tip  1

Contents of the file Unclewill.txt:

Programming and Problem Solving With C++, 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
6. What is Race condition? How to prevent it? [2 marks] 7. How many synchronization methods do you know and compare the differences. [2 marks] 8. Explain what are the “mutual exclusion”, “deadlock”, “livelock”, and “eventual entry”, with the traffic intersection as an example like dinning philosophy. [2 marks] 9. For memory allocation, what are the difference between internal fragmentation and external fragmentation. Explain with an example. [2 marks] 10. How can the virtual memory map to the physical memory. Explain with an example. [2 marks]
Your answers normally have 50 words. Less than 50 words will not get marks. 1. What is context switch between multiple processes? [2 marks] 2. Draw the memory layout for a C program. [2 marks] 3. How many states does a process has? [2 marks] 4. Compare the non-preemptitve scheduling and preemptive scheduling. [2 marks] 5. Given 4 process and their arrival times and next CPU burst times, what are the average times and average Turnaround time, for different scheduling algorithms including: a. First Come, First-Served (FCFS) Scheduling [2 marks] b. Shortest-Job-First (SJF) Scheduling [2 marks] c. Shortest-remaining-time-first [2 marks] d. Priority Scheduling [2 marks] e. Round Robin (RR) [2 marks] Process Arrival Time Burst Time P1 0 8 P2 1 9 P3 3 2 P4 5 4
a database with multiple tables from attributes as shown above that are in 3NF, showing PK, non-key attributes, and FK for each table? Assume the tables are already in 1NF. [Hint: 3 tables will result after deducing 1NF -> 2NF -> 3NF]

Chapter 7 Solutions

Programming and Problem Solving With C++

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