MENU

Chapter 6 Data File Handling Solutions

Question - 1 : -
Write a user defined function word_count() in C+ + to count how many words are present in a text file named “opinion.txt”. For Example, if the file opinion.txt contains following text:

Co-education system is necessary for a balanced society. With co-education system. Girls and Boys may develop a feeling of mutual respect towards each other.

The function should display the following :
Total number of words present in the text file are:24

Answer - 1 : -

void word_count ()
 {
 ifstream i;char ch[20];int c=0;
 i.open("opinion.txt"); while(!i.eof())
 {
 i>>ch;
 C=C+1;
 }
 cout<<"Total number of words present in the text file are: "<
 }

Question - 2 : -
Write a function in C + + to count and display the no of three letter words in the file “VOWEL.TXT”.
Example :
If the file contains :
A boy is playing there. I love to eat pizza. A plane is in the sky.
Then the output should be : 4

Answer - 2 : -

#include
 #include
 void wordcount()
 { char word[80];
 int cnt=0;
 ifstream fl;
 f1.open("VOWEL.TXT");
 while (fl>>word)
 {
 if (strlen(word) == 3)
 {
 cnt++;
 cout<
 }
 }
 cout<<" number of three letter words = ”<
 f1.close();
 }

Question - 3 : -
Write a function AECount() in C++, which should read each character of a text file NOTES. TXT, should count and display the occurrence of alphabets A and E (including small cases a and e too).
Example :
If the file content is as follow :
CBSE enhanced its CCE guidelines further.
The AECount() function should display the output as
A:1
E:7

Answer - 3 : -

Void AECount ()
 {
 Fstream obj;
 Obj.open ("NOTES.TXT", ios :: in) ;
 char x;
 int i=o, Sum A=0, Sum E=0,
 while (obj.get (ch)!=0)
 {
 obj . get (x) ;
 if (x=="A"||? x=="a")
 SumA =SumA+l;
 if(x=="E"|| x =="e")
 SumE=sumE+l;
 }
 cout<<"A:"<
 Cout<<"E:"<< SumE;
 }

Question - 4 : -
Write a function Countaroma() to count and display the number of times “Aroma” occurs in a text file “Cook.txt”.

Answer - 4 : -

Void Countword ()
 {
 fstream tfile;
 clrscr();
 tfile.open ("Cook.txt", ios::in);
 char arr[80];
 char ch;
 int i=0;sum=0;n=0;
 while(tfile)
 {
 tfile.get (ch)
 arr[i] = ch;
 i++ ;
 if (strcpy(ch, "Aroma"))
 {
 i--;
 sum = sum+i;
 n++;
 }
 }
 Cout<<"Total number of Aroma:" <
 }

Question - 5 : -
Write a function EUCount ( ) in C++, which should read each character of a text file IMP TXT, should count and display the occurrence of alphabets E and U (including small cases e and u too).
Example:
If the file content is as follows ;
Updated information is simplified by official websites.
The EUCount() function should display the
output as
E:4
U:1

Answer - 5 : -

void EUCount ()
 {
 fstream obj;
 obj. open("IMP.TXT");
 char ch;
 int i = 0 , sumE=0, sumU=0 ;
 while (obj. get (ch)! =0)
 {obj. get (ch);
 if(ch=="E"||ch=="e")
 SumE=sumE+l;
 if(ch=="U" ??ch =="u")
 SumU=SumU+l;
 } cout << "E : " «sum E<
 cout<<"U:"<
 }

Question - 6 : -
Write a function CountDig() in C++ which reads the content of a text file story.txt and displays the number of digits in it
For example if the file contains:
Amrapali was a queen of Gareware Kingdom in the year 1911. She had 2 daughters.
Her palace had 200 rooms.
Then the output on the screen should be :
Number of digits in Story: 8

Answer - 6 : -

void CountDig ()
 {
 ifstream fin ("STORY.TXT");
 char ch;
 int count=0;
 while(!fin.eof ())
 {fin>>ch;
 if(isdigit(ch))
 count++;
 }
 cout<<"Number of digit in Story: "<
 fin:close();
 }

Question - 7 : -
Write a function in C + + to count the number of lines starting with a digit in a text file “Diary.Txt”.

Answer - 7 : -

int countNum ()
 {
 ifstream fin("Diary.Txt");
 char ch[80];
 int count = 0;
 while (!fin.eof())
 {
 fin.getline (ch, 80);
 if (isdigit (ch[0])
 count ++;
 }
 fin.close( );
 return count;
 }

Question - 8 : -
Write a function in C+ + to read the content of a text file “DELHI.TXT” and display all those lines on the screen, which are either starting with ‘D’ or starting with ‘M’.

Answer - 8 : -

void disp ()
 {
 ifstream FILE("DELHI.TXT");
 int CA= 0;
 char LINE [80];
 while(FILE.getline(LINE,80))
 if(LINE [0]=='D' | | LINE[0]= 'M')
 puts(LINE);
 FILE.close();
 }

Question - 9 : -
Write a function in C++ to read the content of a text file “PLACES.TXT” and display all those lines on screen, which are either starting with ‘P’ or starting with ‘S’.

Answer - 9 : -

void disp ()
 {
 ifstream FILE("PLACES.TXT");
 int CA=0;
 char LINE [80];
 while(FILE.getline(LINE,80))
 if(LINE[0]=='P' | | LINE[0]=='S')
 puts(LINE);
 FILE.close();
 }

Question - 10 : -
Write a function CountYouMe ( ) in C+ + which reads the contents of a text file story.txt and counts the words You and Me (not case sensitive).
For example, if the file contains:
You are my best friend.
You and me make a good team.
The function should display the output as:
Count for you : 2
Count for Me : 1

Answer - 10 : -

void countyoume ( )
 {
 ifstream fil("story.txt");
 char word [80];
 int WC = 0, WM = 0;
 while (! fil.eof())
 {
 fil>>word;
 if ((strcmp WC++ ; (word, "You") = = 0)
 if (strcmp (word, "Me") = = 0) )
 WM++ ;
 }
 cout <<"count for you" << WC;
 cout <<"count for me" << WM;
 fil. close ( ) ;}

×