In: Computer Science
C++
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.
//Cpp program to count Uppercase, Lowercase, Digits count from
file
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
//ifstream For reading file purpose only
ifstream file("text.txt");
//cheks whether given file is present or not
if(!file){
cout<<"File not Found";
return 0;
}
//variables for all three counts and character in
file
char character;
int uppercaseCount=0, lowercaseCount=0, digitsCount=0;
//loop for reading and checkile file character by
character
while(file){
//get a single character
file.get(character);
//checks uppercase character
if((character >= 'A') && (character <= 'Z'))
uppercaseCount++;
//checks lowercase character
else if((character >= 'a') && (character
<= 'z'))
lowercaseCount++;
//checks lowercase character
else if((character >= '0')
&& (character <= '9'))
digitsCount++;
}
cout<<"\nUppercase characters:
"<<uppercaseCount;
cout<<"\nLowercase characters: "<<lowercaseCount;
cout<<"\nDigits: "<<digitsCount;
return 0;
}
/*
content of File text.txt
C++
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.
Output :-
Uppercase characters: 19
Lowercase characters: 434
Digits: 4
*/