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;
}
Answers for C++ Course using "Starting out with C++ : from control structures through objects" by Tony Gaddis. 7/8th ed. I used the Pearson myprogramminglab to complete the homework. Here are my solutions/answers for the exercises/labs so please use the test bank as a GUIDE if you're stuck. Let me know if you find a better solution to a problem or any of the programming challenges. Thanks in advance for your support! Send in missing programming challenges to cplusplus.answers@gmail.com
Solution does not work
ReplyDeleteAfter first #include should be < iostream >
ReplyDeletesecond #include should be < fstream >
No spaces between iostream and fstream and the "< >"