Skip to content

Instantly share code, notes, and snippets.

@luitzenhietkamp
Last active December 12, 2017 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luitzenhietkamp/debe965cbe369d5d11e611e9e633cba3 to your computer and use it in GitHub Desktop.
Save luitzenhietkamp/debe965cbe369d5d11e611e9e633cba3 to your computer and use it in GitHub Desktop.
// Exercise 3-3
// A little program to count how many times
// each distinct word appears in its input.
// Note: the program is not able to count
// words with and without capital letters
// as being the same. It will also distinguish
// words based on whether they are proceded or
// followed by interpunction. Considering the
// scope of this chapter, that is ok however.
#include<algorithm>
#include<ios>
#include<iostream>
#include<string>
#include<vector>
using std::cout; using std::cin;
using std::endl; using std::vector;
using std::string; using std::sort;
int main(){
// ask for the users name
cout << "Please enter your first name: " ;
string name ;
cin >> name ;
cout << "Hello, " << name << "!" << endl ;
// ask for and read a text or list of words
cout << endl << "Please enter a text or a list of words, "
"followed by end-of-file: " ;
vector<string> entered_words ;
string x ;
// invariant: entered_words contains all the words entered so far
while(cin >> x) {
entered_words.push_back(x) ;
}
// check that the user has entered some words at all
typedef vector<string>::size_type vec_ssz ;
vec_ssz size = entered_words.size() ;
if (size == 0) {
cout << endl << "Aah, come on man. At least enter some words. Try again." ;
return 1 ;
}
// sort the words
// after this words that are entered multiple times will be next to each other
// we can then check if each word has been entered before by comparing it to
// the previous word in the vector
// since we have not learned anything yet about removing entries from vectors,
// we will then create new vectors to write individual words and another one to
// count occurrences
sort(entered_words.begin(), entered_words.end()) ;
// define vectors and vector sizes
vector<string> unique_words ;
vec_ssz unique_sz ;
vector<int> tally ;
typedef vector<int>::size_type vec_isz ;
vec_isz tally_sz ;
// invariant: we have analyzed count items in the vector entered_words so far
for(vec_ssz count = 0; count != size; ++count) {
if (count == 0) {
unique_words.push_back(entered_words[0]) ;
tally.push_back(1) ;
} else if (entered_words[count] == unique_words[unique_sz - 1] ) {
++tally[tally_sz - 1] ;
} else {
unique_words.push_back(entered_words[count]) ;
tally.push_back(1) ;
}
// Compute the size of the unique_words en tally vectors
unique_sz = unique_words.size() ;
tally_sz = tally.size() ;
}
// invariant: we have written count words and their statistics to the output
// stream so far
for(int count = 0; count != unique_sz; ++count) {
cout << unique_words[count] << ": used " << tally[count] << " time(s)" << endl ;
}
return 0 ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment