Data File Handling - Text File (SD/4/B/121)

Write a function in C++ to count the no. of "He" or "She" words present in a text file "STORY.TXT".

Example: In the file "STORY.TXT" content is as follows:

He is playing cricket.
She is playing badminton.
She loves icecream.
He loves fruits.

The output of the function should be as:

Total count of He/ She in file : 4




Click Here for Solution






2 comments:

  1. Another Way to Solve This Program by Character Reading in C++

    #include
    #include

    void store();
    void read();

    void main()
    {
    clrscr();
    //Storing Data
    store();
    //Reading Data
    read();
    getch();
    }
    void store()
    {
    char name[80];
    cout<<"Enter Your Paragraph : ";
    cin.getline(name,80);
    ofstream fout;
    fout.open("kabeer.txt",ios::app);
    fout<<name<<endl;
    fout.close();
    }
    void read()
    {
    char ch;
    int he=0,she=0;
    clrscr();
    ifstream fin;
    fin.open("kabeer.txt",ios::in);
    ch=fin.get();
    while(!fin.eof())
    {
    cout<<ch;
    if(ch=='h'||ch=='H')
    {
    ch=fin.get();
    if(ch=='E'||ch=='e')
    {
    he++;
    }
    }
    else if(ch=='s'||ch=='S')
    {
    ch=fin.get();
    if(ch=='h'||ch=='H')
    {
    cout<<ch;
    ch=fin.get();
    if(ch=='e'||ch=='E')
    {
    she++;
    }
    }
    }
    else
    ch=fin.get();
    }
    fin.close();
    cout<<endl<<"He Count : "<<he<<endl<<"She Count : "<<she;
    }

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

Post your comments

Powered by Blogger.