ahmed0saber

Count the number of words in a file in C++

Nov 4th, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int main()
  5. {
  6.  ifstream fin("aaaaa.txt");
  7.  int word=1;
  8.  char ch;
  9.  fin.seekg(0,ios::beg);
  10.  
  11.  while(fin)
  12.  {
  13.   fin.get(ch);
  14.   if(ch==' '||ch=='\n')
  15.    word++;
  16.  }
  17.  
  18.  cout<<"\nWords="<<word<<"\n";
  19.  fin.close();
  20.  
  21.  return 0;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment