You are on page 1of 5

PROGRAM- 9

OBJECTIVE
Develop a menu based C++ program to enter 5 lines of text into a new file
-story.txt and do the followinga)
b)
c)
d)
e)

To
To
To
To
To

display
display
display
display
display

the
the
the
the
the

text in uppercase.
number of alphabets in the file
number of special characters in the file
number of words present in the file.
number of lines in the file.

SOURCE CODE
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<fstream.h>
void add()
{
ofstream f("story.txt");
f<<"There was once a lazy boy.....\n";
f<<"His name was Jake Grim\n";
f<<"All the people in the town talked of his laziness\n";
f<<"Once an old man came to him and said- \n";
f<<"\"Don\'t die before you\'re dead\"\n";
f<<"TO BE CONTINUED";
}
void upper()
{
ifstream f("story.txt");
if(!f)
cout<<"FILE NOT FOUND";
else
{
char ch;
while(!f.eof())
{
ch=f.get();
ch=toupper(ch);
cout<<ch;
}
}
f.close();
}
void countalpha()

ifstream f("story.txt");
if(!f)
cout<<"FILE NOT FOUND";
else
{
char ch;
int c=0;
while(!f.eof())
{
ch=f.get();
if(isalpha(ch))
c++;
}
cout<<"\nNo: of alphabets:"<<c;
}
f.close();

void countspecial()
{
ifstream f("story.txt");
if(!f)
cout<<"FILE NOT FOUND";
else
{
char ch;
int sp=0;
while(f.get(ch))
{
if(!isalnum(ch))
sp++;
}
cout<<"\nNo: of special characters:"<<sp;
}
f.close();
}
void countwords()
{
ifstream f("story.txt");
if(!f)
cout<<"FILE NOT FOUND";
else
{
char ch[15];
int c1=0;
while(f>>ch)
{
c1++;
}
cout<<"\nNo: of words :"<<c1;

}
f.close();
}
void countlines()
{
ifstream f("story.txt");
if(!f)
cout<<"FILE NOT FOUND";
else
{
char ch[200];
int c=0;
while(f.getline(ch,200))
{
c++;
}
cout<<"\nNo: of lines :"<<c;
}
f.close();
}
void menu()
{
int ch;
cout<<"\nPlease select your option:";
cout<<"\n1. Display the text in uppercase";
cout<<"\n2. Display the number of alphabets";
cout<<"\n3. Display the number of special characters";
cout<<"\n4. Display the number of words in the file";
cout<<"\n5. Display the number of lines";
cout<<"\n6. Exit";
cout<<"\nEnter your option:";
cin>>ch;
switch(ch)
{
case 1 : upper();
break;
case 2 : countalpha();
break;
case 3 : countspecial();
break;
case 4 : countwords();
break;
case 5 : countlines();
break;
case 6 : exit(0);
break;
default:cout<<"Wrong Option";
}
}

int main()
{
add();
st:
menu();
goto st;
getch();
return 0;
}

OUTPUT
Please select your option:
1. Display the text in uppercase
2. Display the number of alphabets
3. Display the number of special characters
4. Display the number of words in the file
5. Display the number of lines
6. Exit
Enter your option:1
THERE WAS ONCE A LAZY BOY.....
HIS NAME WAS JAKE GRIM
ALL THE PEOPLE IN THE TOWN TALKED OF HIS LAZINESS
ONCE AN OLD MAN CAME TO HIM AND SAID"DON'T DIE BEFORE YOU'RE DEAD"
TO BE CONTINUED
Enter your option:2
No: of alphabets:141
Enter your option:3
No: of special characters:48
Enter your option:4
No: of words :38

Enter your option:5


No: of lines :6

You might also like