Write a program that reads the contents of a file named text.txt and determines
ID: 3642582 • Letter: W
Question
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.
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
//PROTOTYPES
bool isUppercase(char);
bool isLowercase(char);
bool isDigit(char);
int main()
{
//LOCAL DECLARATIONS
fstream fin("text.txt", ios::in);
char word[40];
int upper = 0;
int lower = 0;
int digit = 0;
if (fin)
{
while (fin >> word)
{
for (int i = 0; word[i] != ''; i++)
{
if (isUppercase(word[i]))
{
upper++;
}
else if (isLowercase(word[i]))
{
lower++;
}
else if (isDigit(word[i]))
{
digit++;
}
}
}
fin.close();
cout << "Uppercase characters: " << upper << endl;
cout << "Lowercase characters: " << lower << endl;
cout << "Digits: " << digit << endl;
}
else
{
cout << "Cannot open text.txt" << endl;
}
cin.sync();
cin.get();
return 0;
}
//---------------------------------------------------------
// FUNCTION DEFINITIONS
//---------------------------------------------------------
bool isUppercase(char chr)
{
return chr >= 'A' && chr <= 'Z';
}
//---------------------------------------------------------
bool isLowercase(char chr)
{
return chr >= 'a' && chr <= 'z';
}
//---------------------------------------------------------
bool isDigit(char chr)
{
return chr >= '0' && chr <= '9';
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.