StudyLover
  • Home
  • Study Zone
  • Profiles
  • Contact us
  • Sign in
StudyLover 24. Program to count the number of lines, words, and characters in a file.
Download
  1. C++ Programs list
23. Program to read content from one file and write it to another file. : 25. Program to search for a specific word in a file and count its occurrences.
C++ Programs list

1.      Program to count the number of lines, words, and characters in a file.

  1. #include <iostream.h>

  2. #include <fstream.h>

  3. #include<conio.h>

  4. int main() {

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

  6.     int lines = 0, words = 0, chars = 0;

  7.     char ch;

  8.     while (fin.get(ch)) {

  9.                 chars++;

  10.                 if (lines==0){lines=1}

  11.                 if (ch == '\n') {

  12.                     lines++;

  13.                     words++;

  14.                 } else if (ch==' ') {

  15.                     words++;

  16.                 }

  17.     }

  18.     // Handle the last word if the file doesn't end with a newline

  19.     if (chars > 0 && ch!=' ') {

  20.                 words++;

  21.     }

  22.     fin.close();

  23.     cout << "Lines: " << lines << endl;

  24.     cout << "Words: " << words << endl;

  25.     cout << "Characters: " << chars << endl;

  26.     getch();

  27.     return 0;

  28. }

  29.  

23. Program to read content from one file and write it to another file. 25. Program to search for a specific word in a file and count its occurrences.
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