Created
December 13, 2017 12:30
-
-
Save luitzenhietkamp/24461d37409823a84585eaa958798ada to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 3-4 | |
// A little program to find the longest string(s). | |
// Note: the program does not check for strings that | |
// are entered more than once and will treat each | |
// instance separately. Improving the program such | |
// that each instance is treated separately shouldn't | |
// be too complicated, but since the minimum of the | |
// exercise has been met and since basically the same | |
// thing has been done in the previous exercise, we | |
// leave it at that. | |
// Furthermore, it has the same limitations as the | |
// program in the previous exercise, which are beyond | |
// the scope of the chapter. | |
// After I finished the exercise I realised that I was | |
// also asked to do the same for the shortest string. | |
// Since the solution is essentially the same I will | |
// forego on implementing it. | |
#include<iostream> | |
#include<string> | |
#include<vector> | |
using std::cout; using std::cin; | |
using std::endl; using std::vector; | |
using std::string; | |
int main(){ | |
// ask for the user's 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 ; | |
} | |
// create a variable to hold the length of the longest string | |
size_t sz_longest_word ; | |
// check whether each string is longer than the previously longest string | |
// invariant: we have checked count strings so far | |
for (vec_ssz count = 0; count != size; ++count) { | |
size_t string_sz = entered_words[count].size() ; | |
if (count == 0 || string_sz > sz_longest_word) { | |
sz_longest_word = string_sz ; | |
} | |
} | |
// write the output message | |
cout << endl << "The largest string is " << sz_longest_word << " characters long." | |
<< endl << "The following strings are " << sz_longest_word << " characters long:" ; | |
// output the string or strings that is/are the longest | |
// invariant: we have checked for count strings whether they match the criterium | |
for(vec_ssz count = 0; count != size; ++count){ | |
size_t string_sz = entered_words[count].size() ; | |
if (string_sz == sz_longest_word){ | |
cout << endl << entered_words[count] ; | |
} | |
} | |
return 0 ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment