Advertisement
kartikkukreja

Counting the number of lines, words and characters in a file

Apr 28th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. /*  Author : Kartik Kukreja
  2.     Roll No: 2K10/CO/049
  3.     Description :   Program to count the number of character, words and lines in a given file.
  4.                     The input file is named "input.txt" and the output is written to console.
  5.  */
  6.  
  7. #include <cstdio>
  8. #include <fstream>
  9. #include <cstring>
  10. using namespace std;
  11.  
  12. int main()  {
  13.     int countC = 0, countW = 0, countL = 0;
  14.     char str[10000], *p;
  15.  
  16.     ifstream fin("input.txt");
  17.     while(!fin.eof())   {
  18.         fin.getline(str, 10000);
  19.         countL++;
  20.         p = strtok(str, " ");
  21.         while(p)    {
  22.             countW++;
  23.             countC += strlen(p);
  24.             p = strtok(NULL, " ");
  25.         }
  26.     }
  27.     fin.close();
  28.  
  29.     printf("No. of characters : %d\n", countC);
  30.     printf("No. of words : %d\n", countW);
  31.     printf("No. of lines : %d\n", countL);
  32.  
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement