Write a C++ program to create a file, write some data in it through program and then perform following operations:
a) count no of uppercase alphabets.
b) count no of lowercase alphabets.
c) count no of digits.
d) count no of words.
e) count no of lines.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ofstream out("Test.txt");
out << "London is the capital and largest city of England and the United Kingdom.\n"
<< "It stands on the River Thames in south-east England at the head of a 50-mile\n"
<< "(80 km) estuary down to the North Sea, and has been a major settlement for two\n"
<< "millennia The City of London, its ancient core and financial centre, was founded\n"
<< "by the Romans as Londinium and retains boundaries close to its medieval ones.\n";
out.close();
ifstream in("Test.txt");
string line;
int lineCount=0;
int wordsCount=0;
int upAlph=0;
int lowAlph=0;
int dig=0;
while (getline(in, line, '\n'))
{
bool isWord = false;
for (int i = 0; i < line.size(); i++)
{
if (line[i] >= 'A'&&line[i] <= 'Z')
upAlph++;
else if (line[i] >= 'a'&&line[i] <= 'z')
lowAlph++;
else if (isdigit(line[i]))
dig++;
if (line[i] == ' ' || line[i] == '\n' || line[i] == '\t')
isWord = false;
else if (isWord==false)
{
isWord = true;
wordsCount++;
}
}
lineCount++;
}
cout << "\nNomber of uppercase alphabets is " << upAlph
<< "\nNomber of lowercase alphabets is " << lowAlph
<< "\nNomber of digits is " << dig
<< "\nNomber of words is " << wordsCount
<< "\nNomber of lines is " << lineCount;
}
Comments
Leave a comment