Guest User

Untitled

a guest
Dec 11th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. //telja.cpp
  2. //Author: Valdís Hrund Einarsdóttir
  3. //email: valdis12@ru.is
  4. /*
  5. Description: A program that counts words in a text file.
  6. */
  7. #include <iostream>
  8. #include <fstream>
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.     int wordCount = 0;
  14.     ifstream text;
  15.     char filename[100];
  16.     char first(' '), second(' ');
  17.     cout << "---------------------------------" << endl;
  18.     cout << "           Word Count" << endl;
  19.     cout << "---------------------------------" << endl;
  20.     cout << "Enter filename: " ;
  21.     cin >> filename;
  22.  
  23.     text.open(filename);
  24.  
  25.     //Opens text file and reads every letter one at a time in to first
  26.     while(text.get(first))
  27.     {   //This counts all instances of spaces followed by letters which corresponds to the number of words.
  28.         if (isalpha(first) && isspace(second))
  29.             wordCount++;
  30.         second = first;
  31.     }
  32.     cout << "Number of words: " << wordCount;
  33.     return 0;
  34. }
Add Comment
Please, Sign In to add comment