Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //telja.cpp
- //Author: Valdís Hrund Einarsdóttir
- //email: valdis12@ru.is
- /*
- Description: A program that counts words in a text file.
- */
- #include <iostream>
- #include <fstream>
- using namespace std;
- int main()
- {
- int wordCount = 0;
- ifstream text;
- char filename[100];
- char first(' '), second(' ');
- cout << "---------------------------------" << endl;
- cout << " Word Count" << endl;
- cout << "---------------------------------" << endl;
- cout << "Enter filename: " ;
- cin >> filename;
- text.open(filename);
- //Opens text file and reads every letter one at a time in to first
- while(text.get(first))
- { //This counts all instances of spaces followed by letters which corresponds to the number of words.
- if (isalpha(first) && isspace(second))
- wordCount++;
- second = first;
- }
- cout << "Number of words: " << wordCount;
- return 0;
- }
Add Comment
Please, Sign In to add comment