CH10: Programming Challenge #15

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.

Solution (By Raezorane R.)

// Chapter 10, Programming Challenge 15: Character Analysis
#include 
#include 
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;


2 comments:

  1. Solution does not work

    ReplyDelete
  2. After first #include should be < iostream >
    second #include should be < fstream >
    No spaces between iostream and fstream and the "< >"

    ReplyDelete