Question: 15
Write a function in c++ named count_a() to read a file named “story.txt”. Count and print total number of words starting with ‘a’ or ‘A’ in the file.
Sol:
#include<iostream>> #include<fstream> #include<stdlib.h> #include<string.h> using namespace std; void count_a() { ifstream pqr; char word[30]; int c=0; pqr.open("story.txt"); if (pqr.fail()) { cout<<"Unable to open the file"<<endl; exit(1); } while(!pqr.eof()) { pqr>>word; if(word[0]=='a' || word[0]=='A') { cout<<word<<endl; c++; } } pqr.close(); cout<<"total count "<<c<<endl; } int main() { /* clrscr(); */ count_a(); return(0); }
Question: 16
Write a function in c++ named count_a() to read a file named “story.txt”. Count and print total words not starting with “a” or “A” the file file.
Sol:
#include<iostream>> #include<fstream> #include<stdlib.h> #include<string.h> using namespace std; void count_a() { ifstream pqr; char word[30]; int c=0; pqr.open("story.txt"); if (pqr.fail()) { cout<<"Unable to open the file"<<endl; exit(1); } while(!pqr.eof()) { pqr>>word; if(!(word[0]=='a' || word[0]=='A')) { cout<<word<<endl; c++; } } pqr.close(); cout<<"total count "<<c<<endl; } int main() { /* clrscr(); */ count_a(); return(0); }
Question: 17
Write a function in c++ named count() to read a file named “story1.txt”. Count and print total number of words starting with vowels present in the file.
/* count total words starting with ‘a’,’e’,’i’,’o’,’u’ or
‘A’,’E’,’I’,’O’,’U’ in the file */
Sol:
#include<iostream> #include<fstream> #include<stdlib.h> #include<string.h> using namespace std; void count() { ifstream pqr; char word[30]; int c=0; pqr.open("story.txt"); if (pqr.fail()) { cout<<"Unable to open the file"<<endl; exit(1); } while(!pqr.eof()) { pqr>>word; if(word[0]=='a' || word[0]=='A' || word[0]=='e' || word[0]=='E' || word[0]=='i' || word[0]=='I' || word[0]=='o' || word[0]=='O' || word[0]=='u' || word[0]=='U') { cout<<word<<endl; c++; } } pqr.close(); cout<<"total count "<<c<<endl; } int main() { /* clrscr(); */ count(); return(0); }