10.15: Character Analysis Write a program that reads the contents of a file name
ID: 670561 • Letter: 1
Question
10.15: Character Analysis
Write a program that reads the contents of a file named text.txt and determines the following:
The number of uppercase letters in the file
The number of lowercase letters in the file
The number of digits in the file
Prompts And Output Labels. There are no prompts-- nothing is read from standard in, just from the file text.txt. Each of the numbers calculated is displayed on a separate line on standard output , preceded by the following prompts (respectively): "Uppercase characters : ", "Lowercase characters : ", "Digits: ".
Input Validation. None.
In C++ Please, Thanks.
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char ch; // To hold a character from the file
// Counters for the number of uppercase characters,
// lowercase characters, and digits found in the file.
int uppercase = 0;
int lowercase = 0;
int digits = 0;
// Open the file.
ifstream inputFile;
inputFile.open("text.txt");
// Read each character in the file
// and analyze it.
while (inputFile >> ch)
{
if (isupper(ch))
uppercase++;
if (islower(ch))
lowercase++;
if (isdigit(ch))
digits++;
}
// Close the file.
inputFile.close();
// Display the results.
cout << "Uppercase characters: " << uppercase << endl;
cout << "Lowercase characters: " << lowercase << endl;
cout << "Digits: " << digits << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.