Text File Operations for Practice in C++
#include<fstream.h>
#include<conio.h>
#include<string.h>
void ToggleCase()
{
ifstream fin("story.txt",ios::in);
char ch;
while(!fin.eof())
{
fin.get(ch);
if(ch>='A'&&ch<='Z')
cout<<(char)(ch+32);
else if(ch>='a' && ch<='z')
cout<<(char)(ch-32);
else
cout<<ch;
}
cout<<endl;
fin.close();
cout<<"\nThe entire file is displayed in Toggle Case.";
}
void CountDigits()
{
ifstream fin("story.txt",ios::in);
char ch;
int count=0;
while(!fin.eof())
{
fin.get(ch);
if(ch>='0'&&ch<='9')
{
count++;
}
}
cout<<endl;
fin.close();
cout<<"\nThere were "<<count<<" digits in the file.\n";
}
void Wordof4Letters()
{
ifstream fin("story.txt",ios::in);
char word[80];
int count=0;
while(!fin.eof())
{
fin.getline(word,80,' ');
if(strlen(word)==4)
{
count++;
cout<<count<<": "<<word<<" ";
}
}
cout<<endl;
fin.close();
cout<<"\nThere were "<<count<<" words of 4 characters in the file.\n";
}
void LineWithCapLetter()
{
ifstream fin("story.txt",ios::in);
char line[80];
int count=0;
while(!fin.eof())
{
fin.getline(line,80,'\n');
if(line[0]>='A' && line[0]<='Z')
{
count++;
cout<<line<<endl;
}
}
cout<<endl;
fin.close();
cout<<"\nThere were "<<count<<" lines starting with Capital Letter in the file.\n";
}
void main()
{
int ch;
do
{
clrscr();
cout<<"Text File Operations\n";
cout<<"====================\n";
cout<<"0. Exit.\n";
cout<<"1. Count & Print 4 Letters Word in a File.\n";
cout<<"2. Count & Print Line with Capital Letter Started\n";
cout<<"3. Total Digits in the File\n";
cout<<"4. Display File in Toggle Case\n";
cout<<"Enter your Choice: ";
cin>>ch;
switch(ch)
{
case 1: Wordof4Letters(); break;
case 2: LineWithCapLetter(); break;
case 3: CountDigits(); break;
case 4: ToggleCase(); break;
}
getch();
}while(ch);
}
#include<conio.h>
#include<string.h>
void ToggleCase()
{
ifstream fin("story.txt",ios::in);
char ch;
while(!fin.eof())
{
fin.get(ch);
if(ch>='A'&&ch<='Z')
cout<<(char)(ch+32);
else if(ch>='a' && ch<='z')
cout<<(char)(ch-32);
else
cout<<ch;
}
cout<<endl;
fin.close();
cout<<"\nThe entire file is displayed in Toggle Case.";
}
void CountDigits()
{
ifstream fin("story.txt",ios::in);
char ch;
int count=0;
while(!fin.eof())
{
fin.get(ch);
if(ch>='0'&&ch<='9')
{
count++;
}
}
cout<<endl;
fin.close();
cout<<"\nThere were "<<count<<" digits in the file.\n";
}
void Wordof4Letters()
{
ifstream fin("story.txt",ios::in);
char word[80];
int count=0;
while(!fin.eof())
{
fin.getline(word,80,' ');
if(strlen(word)==4)
{
count++;
cout<<count<<": "<<word<<" ";
}
}
cout<<endl;
fin.close();
cout<<"\nThere were "<<count<<" words of 4 characters in the file.\n";
}
void LineWithCapLetter()
{
ifstream fin("story.txt",ios::in);
char line[80];
int count=0;
while(!fin.eof())
{
fin.getline(line,80,'\n');
if(line[0]>='A' && line[0]<='Z')
{
count++;
cout<<line<<endl;
}
}
cout<<endl;
fin.close();
cout<<"\nThere were "<<count<<" lines starting with Capital Letter in the file.\n";
}
void main()
{
int ch;
do
{
clrscr();
cout<<"Text File Operations\n";
cout<<"====================\n";
cout<<"0. Exit.\n";
cout<<"1. Count & Print 4 Letters Word in a File.\n";
cout<<"2. Count & Print Line with Capital Letter Started\n";
cout<<"3. Total Digits in the File\n";
cout<<"4. Display File in Toggle Case\n";
cout<<"Enter your Choice: ";
cin>>ch;
switch(ch)
{
case 1: Wordof4Letters(); break;
case 2: LineWithCapLetter(); break;
case 3: CountDigits(); break;
case 4: ToggleCase(); break;
}
getch();
}while(ch);
}
No comments
Post your comments