Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------
- // ************************************************** **************
- //
- // count.cc
- //
- // Introduction: Count lines and words
- // (1) in a keyboard input, or
- // (2) in a file
- //
- // Uses:
- // (1) ./count
- // (2) ./count < input.txt
- //
- // ************************************************** **************
- #include <cstdlib>
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- int main()
- {
- string temp = "";
- string word = "";
- int countWords = 0;
- string line = "";
- int countLines = 0;
- cout << "This program counts lines and words. " << endl;
- cout << "Please give input. Use Ctrl + C to stop." << endl;
- while ( cin )
- {
- getline( cin, line ); // read line
- ++countLines; // count line
- std::istringstream linestream( line );
- while ( !linestream.eof() )
- {
- linestream >> word; // read word
- temp = word;
- ++countWords; // count word
- }
- cout << "Words: " << countWords << endl;
- cout << "Lines: " << countLines << endl;
- }
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment