Concept explainers
Program plan:
- Declare a variable file of ifstream type to read the input from the file.
- Declare a variable letter of type char to store the letter read from the file.
- Declare 6 variables upperCounter, lowerCounter, blank, digit, punctuation, allElseCounter of type int to store the counter of each type.
- Declare a variable inFile of type String to store the name of the file read from the user.
- A do- while loop is used to read letters from the file and to increment the each type of counter.
- 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.
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:
Contents of the file Unclewill.txt:
Want to see more full solutions like this?
Chapter 7 Solutions
Programming and Problem Solving With C++
- c++ program : the program will be able to show how many words in the file.txt. And be able to show the first 10 words and the number of the occurences of each word . I attached a pic there in the beginning showing how many word in a file.txt ( it's not necessary that you have to choose file with 2000 words) and then show first 10 words and how many were of them in the file. The file will contain signs and numbers as well but we will not count them. We can use the erase function . Any file or we can write a few lines there a few repated , numbers and signs like , or : or ?arrow_forwardWrite a function count_character(filename, character) that takes in a filename and a character, both as strings, and returns the total number of times the given character occurs in the file. Examples: Contents of hello.txt:HelloHELLOHelloHELLO count_character('hello.txt', 'L') returns 4count_character('hello.txt', 'H') returns 4count_character('hello.txt', 'e') returns 2 Requirements: Please use Python Programming only Show screenshot of your work.arrow_forwardI have a text file and I want to read it in C++. variables to use: fname, lname , Id nr, Gender and all third line which is an address The file is : Tommy James 23456731667 Streetname 45 , 154 23 Georgia John Andersson 3456723464 Streetname 12 , 231 32 Las Vegas Kim Larsson 3456312653 Streetnamn 15, 231 56 Florida If we Said the 8th digit in ID number is showing the Gender of the person. By %2== 0 it's Male The output should be: Lname Fname [F] address Is there possible to create a program in C++ with the previously output. lname , fname [F]Adress Without have a problem with Las Vegas word. I have a hard time to figure how I can do the third line in the file. They read to word Las and then the compiler shows terminate called after throwing an instance of ' std::invalid_ argument ' what (): stoi aborted so I figured that the problem I have in one line the city is two word.arrow_forward
- The problem: You are asked to write a text analyzer program that reads a text file and counts the number of lines, number of words (for example, King′s counts as one word) and the frequency of occurrence of each of the 26 letters of the alphabet. NOTE: • We ignore case: Treat upper and lowercase letters to be the same. • Assume that there is at least one space between words on the same line. • Only characters A-Z are to be counted. For example, if the text file TextAnalyzerData contained this text shown below: Baa, baa, black sheep Have you any wool? Yes sir, yes sir Three bags full. One for my master And one for the dame One for the little boy Who lives down the lane. The Analyzer should print the following information: Number of Lines: 8 Number of Words: 34 Letter Counts Frequency of A: 12 Frequency of B: 5 Frequency of C: 1 Frequency of D: 3 Frequency of E: 18 Frequency of F: 4 Frequency of G: 1 Frequency of H: 7 Frequency of I: 4…arrow_forwardAssume that infile has been properly defined to read data from a file and that a file of integers has been properly opened. If you don't know how many integers are in the file, which loop(s) would be appropriate to read in all the values from the file? Variable n has been defined as an int. 1. while(! infile.eof()) 2. for(int x = 0; x < 1000000; x++) 3. while(infile >> n) Answers: A. Both 2 and 3 B. 1, 2, and 3 C. Both 1 and 2 D. Both 1 and 3 E. 3 onlyarrow_forwardWrite a program that reads by asking the user the name of the file. After opening it, read it into the structure of array. The input file is in the format: firstName lastName age result (Contents of the input file) The Temp 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 Edward Newman 61 3 1 2 2 2 3 5 3 5 3 5 1 3 1 4 3 5 2 5 3 5 1 3 1 5 1 5 3 1 1 3 1 3 1 3 3 5 2 1 2 3 5 1 1 3 3 5 3 1 5 The program reads the result into the structure called TheResult. The constants and the structure to be used is given below: const int EXTROVERSION = 0; const int AGREEABLENESS = 1; const int CONSCIENTIOUSNESS = 2; const int NEUROTICISM = 3; const int OPEN_TO_EXPERIENCE = 4; const int NUM_QUESTIONS = 50; const int NUM_RESULT = 5; const int MAX_RESULT = 5; struct TheResult { string firstName; string lastName; int age; int answers[NUM_QUESTIONS]; // result double normalizedResult[NUM_RESULT]; }; The name of the input file is…arrow_forward
- Write a program which reads from the keyboard the names of two files containing two doublenumbers. Your program should read these two values from the two files, compute their sum, dif-ference, product and division, and write the results on separate lines into the file “results.txt”.You can safely assume that the input is valid, the two input files exist and each contains one validdouble value.(please solve in C programming language).arrow_forwardProblem: Write a program that accept a character whether a capital or NOT and displays its corresponding number. Filename: character.javaarrow_forward6.20 Word count (c++) Write a program that prompts the user to input the name of a text file and then outputs the number of words in the file. You can consider a "word" to be any text that is surrounded by whitespace (for example, a space, carriage return, newline) or borders the beginning or end of the file.Input Notes: There are two input sources in this program: the text file and standard input. The standard input consists of a single word, the name of the text file to be read. DO NOT USE A STRING VARIABLE.Output Notes (Prompts and Labels): The filename provided in standard input is prompted for with "Enter a file name: " and displayed so that the filename entered is on the same line as the prompt.The output of the program is a single line of the form: "The file contains N words." where N is the number of words determined by the program.arrow_forward
- You are given a file consisting of students’ names in the following form: lastName, firstName, middleName. (Note that some students may not have middle names). Write a program that converts each name to the following form: firstName middleName lastName. Your program must read each student’s entire name in a variable and must consist of a function that takes input as a string, consists of student’s name, and returns the string consisting of the altered name. Use the string function find to find the index of the function length to find the length of the string; and function substr to extract the firstName, middleName, and lastName. Test your program with a file consisting of the following names: Miller, Jason Brian Blair, Lisa Maria Gupta, Anil Kumar Arora, Sumit Sahil Saleh, Rhonda Betharrow_forwardWrite a program that reads the numbers from the file and displays their multiplication, yourprogram should prompt the user to enter a filename. Create such a file and check your code. solve with pyhton.arrow_forwardWrite a program that queries information from three files(given to you). The first file contains the names and telephone numbers of a group of people. The second file contains the names and Social Security numbers of a group of people. The third file contains the names and annual income of a group of people. The groups of people should overlap. Your program should ask the user for a telephone number and then print the name, Social Security number, and annual income, if it can determine that information. Sample run1: Enter the phone number (7 digits, with a dash): 555-1234 555-1234 is associated with Bob Bob's SSN is 000300021 Bob's salary is 55000 Sample run2: Enter the phone number (7 digits, with a dash): 675-4566 Couldn't find a name associated with that number. Sample run3: Enter the phone number (7 digits, with a dash): 000-2345 000-2345 is associated with John John's SSN is 000000004 John's salary is 65000 python languagearrow_forward
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage