StudyLover
  • Home
  • Study Zone
  • Profiles
  • Contact us
  • Sign in
StudyLover 25. Program to search for a specific word in a file and count its occurrences.
Download
  1. C++ Programs list
24. Program to count the number of lines, words, and characters in a file. : 26. Program to implement exception handling with & without the functionality of testing throw restrictions.
C++ Programs list

1.      Program to search for a specific word in a file and count its occurrences.

  1. #include <iostream.h>

  2. #include <fstream.h>

  3. #include <string.h>

  4. #include<conio.h>

  5. int main() {

  6.     ifstream fin("input.txt");

  7.     char word[50], line[100];

  8.     int count = 0;

  9.  

  10.     cout << "Enter the word to search: ";

  11.     cin.getline(word, 50);

  12.  

  13.     while (fin.getline(line, 100)) {

  14.                 char *ptr = strstr(line, word);

  15.                 while (ptr != NULL) {

  16.                     count++;

  17.                     ptr = strstr(ptr + 1, word);

  18.                 }

  19.     }

  20.  

  21.     fin.close();

  22.     cout << "The word '" << word << "' occurs " << count << " times." << endl;

  23.     getch();

  24.     return 0;

  25. }

24. Program to count the number of lines, words, and characters in a file. 26. Program to implement exception handling with & without the functionality of testing throw restrictions.
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website