1. Program to count the number of lines, words, and characters in a file.
#include <iostream.h>
#include <fstream.h>
#include<conio.h>
int main() {
ifstream fin("input.txt");
int lines = 0, words = 0, chars = 0;
char ch;
while (fin.get(ch)) {
chars++;
if (lines==0){lines=1}
if (ch == '\n') {
lines++;
words++;
} else if (ch==' ') {
words++;
}
}
// Handle the last word if the file doesn't end with a newline
if (chars > 0 && ch!=' ') {
words++;
}
fin.close();
cout << "Lines: " << lines << endl;
cout << "Words: " << words << endl;
cout << "Characters: " << chars << endl;
getch();
return 0;
}