Advertisement
kutuzzzov

Untitled

May 19th, 2025
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. int countLines(const std::string& filename) {
  7.     std::ifstream file(filename);
  8.     int lines = 0;
  9.     std::string line;
  10.     while (std::getline(file, line)) {
  11.         ++lines;
  12.     }
  13.     return lines;
  14. }
  15.  
  16. int countWords(const std::string& filename) {
  17.     std::ifstream file(filename);
  18.     int words = 0;
  19.     std::string word;
  20.     while (file >> word) {
  21.         ++words;
  22.     }
  23.     return words;
  24. }
  25.  
  26. int countCharacters(const std::string& filename) {
  27.     std::ifstream file(filename);
  28.     int characters = 0;
  29.     char ch;
  30.     while (file.get(ch)) {
  31.         ++characters;
  32.     }
  33.     return characters;
  34. }
  35.  
  36. void printResults(int lines, int words, int characters) {
  37.     std::cout << "Lines: " << lines << "\n";
  38.     std::cout << "Words: " << words << "\n";
  39.     std::cout << "Characters: " << characters << "\n";
  40. }
  41.  
  42. int main() {
  43.     std::string filename = "example.txt";
  44.  
  45.     int lines = countLines(filename);
  46.     int words = countWords(filename);
  47.     int characters = countCharacters(filename);
  48.  
  49.     printResults(lines, words, characters);
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement