longest, shortest, and string to int

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 8 Exercise 13
Using std_lib_facilities.h by Bjarne Stroustrup.

main.cpp main file

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 8 Exercise 13

#include "std_lib_facilities.h"

struct stringVec
{
	vector<string> vs;
	vector<int> letterCount;
	vector<int> letterCountsorted;
	string longest;
	string shortest;
	string first;
	string last;

public:
	stringVec(vector<string>& _vs) :vs(_vs) {};
	void stringToInt();
	void stringSort();
	void print();
};

void stringVec::stringToInt() {
	for (string x : vs) {
		letterCount.push_back(x.size());
	}
}

void stringVec::stringSort() {
	//longest & shortest
	letterCountsorted = letterCount;
	sort(letterCountsorted);
	int min = letterCountsorted[0];
	int max = letterCountsorted[letterCountsorted.size() - 1];
	int maxIndex;
	int minIndex;
	for (int i = 0; i < letterCountsorted.size(); i++) {
		if (max == letterCount[i]) {
			maxIndex = i;
		}
		if (min == letterCount[i]) {
			minIndex = i;
		}
	}
	longest = vs[maxIndex];
	shortest = vs[minIndex];
	//lexicographical first & last 
	sort(vs);
	first = vs[0];
	last = vs[vs.size() - 1];
}

void stringVec::print() {
	
	for (int x : letterCount) {
		cout << x << "\n";
	}
	cout << "longest: " << longest << "\n";
	cout << "shortest: " << shortest << "\n";
	cout << "first: " << first << "\n";
	cout << "last: " << last << "\n";
}

int main()
try
{
	vector<string> myVec{ "hello", "how", "are", "you", "my", "name", "is" };
	stringVec a(myVec);

	a.stringToInt();

	a.stringSort();

	a.print();

	keep_window_open();

}

catch (runtime_error e) {
	cout << e.what() << "\n";
	keep_window_open();
}
catch (...) {
	cout << "Exiting\n";
	keep_window_open();
}
Output:
5
3
3
3
2
4
2
longest: hello
shortest: is
first: are
last: you
Please enter a character to exit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.