Mix of many program options of text file.

//Mix of many program options of text file.
#include<fstream.h>
#include<ctype.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

//Function Prototyping
void menu(); //Function to run menu driven program.
void options(); //Function passed to menu for use in loop.
void words(); //Function to count words in file.
void length();  //Function to count avg length of words in file.
void fullstop();//Funtion to count total no. of words ending with "." .
void min4();    //Function to count total no. of minimum words of 4 characters.
void countThis();//Function to count this and This in file .
void cons();    //Function to count total consonants and vowels .
void cases();   //Function to change the cases .
void spaces(); //Count & Read the spaces with star sign.
void read(); //Read the text file.

//Global File Object Created
ifstream fin;

void readFile(char *name)
{
system("cls");
system("dir *.txt/w");
cout<<"\nEnter the filename from above list to read it: ";
cin>>name;
}

void words()
{
char file[20],word[20];
int l=0;
readFile(file); //file is taken for filename input which
fin.open(file,ios::in); //is passed to readFile as pointer parameter
while(!fin.eof())
{
fin>>word;
l++;
}
cout<<"Total no. of words : "<<l;
fin.close();
}

void length()
{
int len=0,sum=0,count=0;
char file[20],word[20];
readFile(file);
fin.open(file,ios::in);
while(!fin.eof())
{
fin>>word;
len=strlen(word);
sum+=len;
count++;
}
cout<<endl;
cout<<"Total no. of words         : "<<count<<endl;
cout<<"Average length of words is : "<<sum/count<<endl;
fin.close();
}

void fullstop()
{
int count=0,len=0;
char file[20],word[20];
readFile(file);
fin.open(file,ios::in);
while(!fin.eof())
{
fin>>word;
len=strlen(word);
if(word[len-1]=='.')
{
cout<<word<<endl;
count++;
}

}
cout<<"\nTotal number of words ending with \'.\' is "<<count;
fin.close();
}

void countThis()
{
int count1=0,count2=0;
char file[20],word[20];
readFile(file);
fin.open(file,ios::in);
while(!fin.eof())
{
fin>>word;
if( strcmp(word,"this")==0) count1++;
else if( strcmp(word,"This")==0) count2++;
}
cout<<"\'this\' is used "<<count1 <<" times\n";
cout<<"\'This\' is used "<<count2 <<" times\n";
fin.close();
}

void cons()
{
int con=0,vow=0,dig=0,sp=0;
char file[20],ch;
readFile(file);
fin.open(file,ios::in);
while(!fin.eof())
{
fin.get(ch);
if(isalpha(ch))
{
switch(ch)
{
case 'a': case 'A': case 'e': case 'E':
case 'i': case 'I': case 'o': case 'O':
case 'u': case 'U':
vow++;
default : con++;
}
}
else if(isdigit(ch))
dig++;
else sp++;
}
cout<<"\nTotal no. of Consonants          : "<<con;
cout<<"\nTotal no. of Vowels              : "<<vow;
cout<<"\nTotal no. of Digits              : "<<dig;
cout<<"\nTotal no. of Special Characters  : "<<sp;
fin.close();
}

void cases()
{
char file[20],ch;
readFile(file);
fin.open(file,ios::in);
while(!fin.eof())
{
fin.get(ch);
if(isupper(ch))
cout<<(char)tolower(ch);
else if(islower(ch))
cout<<(char)toupper(ch);
else cout<<ch;
}
fin.close();
}

void space()
{
int count=0;
char file[20],ch;
readFile(file);
fin.open(file,ios::in);
while(!fin.eof())
{
fin.get(ch);
if(ch==' ') { count++; ch='*'; }
cout<<ch;
}
cout<<"\nThe "<<count<<" spaces have been replaced with \'*\'\n";
fin.close();
}

void min4()
{
int count=0,str=0;
char file[20],word[20];
readFile(file);
fin.open(file,ios::in);
while(!fin.eof())
{
fin>>word;
str=strlen(word);
if(str>=4) { count++; cout<<word<<endl; }
}
cout<<"\n\nTotal number of words with minimum 4 characters is "<<count;
fin.close();
}

void read()
{
char file[20],word[20], ch;
readFile(file);
fin.open(file,ios::in);

while(fin)
{
fin.get(ch);
cout<<ch;
}
cout<<"\n\tThe Reading of ";
textcolor(11+BLINK);
cprintf("%s",file);
textcolor(7);
cout<<" is completed...\n";
fin.close();
}

void options()
{
clrscr();
cout<<"\t==========================================================\n";
cout<<"\t\t   PROGRAMS TO READ THE TEXT FILE\n";
cout<<"\t\t\t MIX OF MANY OPTIONS\n";
cout<<"\t==========================================================\n";
cout<<"\t0. Exit................................................\n";
cout<<"\t1. Count Total No. of Words Stored in File.............\n";
cout<<"\t2. Display Average Word Length of File.................\n";
cout<<"\t3. Count Words which end with \'.\'......................\n";
cout<<"\t4. Display all the words with minimum 4 char...........\n";
cout<<"\t5. Count \'this\' and \'This\' in file.....................\n";
cout<<"\t6. Count No. of Consonents, vowels & digits in file....\n";
cout<<"\t7. Display text file Upper to Lower & vice versa.......\n";
cout<<"\t8. Count & Replace spaces with * ......................\n";
cout<<"\t9. Read the File.......................................\n";
cout<<"\tEnter Your Choice : ";
}
void menu()
{
int ch;
do
{
options();
cin>>ch;
switch(ch)
{
case 0: exit(0);
case 1: words(); break;
case 2: length(); break;
case 3: fullstop(); break;
case 4: min4(); break;
case 5: countThis(); break;
case 6: cons(); break;
case 7: cases(); break;
case 8: space(); break;
case 9: read(); break;
default: menu(); break;
}
getch();
}while(ch);
}

void main()
{
clrscr();
menu();
getch();
}




/*Download the cpp file from here...

Download cpp file

*/

No comments

Post your comments

Powered by Blogger.