dictionary alternative

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

//	Philipp Siedler
//	Bjarne Stroustrup's PPP
//	Chapter 11 Exercise 8

#include "std_lib_facilities.h"

void replace_apostrophes(string& _w, vector<string>& _s_words) {
	if (_w == "can't") {
		_w = "cannot";
	}
	if (_w == "won't") {
		_w = "will";
		_s_words.push_back("not");
	}
	if (_w[_w.size() - 1] == 't' &&
		_w[_w.size() - 2] == '\'' &&
		_w[_w.size() - 3] == 'n') {
		string temp = "";
		for (int i = 0; i < _w.size() - 3; i++) {
			temp += _w[i];
		}
		_w = temp;
		_s_words.push_back("not");
	}
}

void process(ifstream& _ist, ofstream& _ost) {
	while (true) {
		//get chars
		char letter;
		vector<char> c_sentence;
		vector<string> s_words;
		if (_ist.eof()) {
			break;
		}
		while (_ist.get(letter)) {
			c_sentence.push_back(letter);
		}

		//transform char vector into strings
		string temp;
		for (int i = 0; i < c_sentence.size(); i++) {
			if(!isspace(c_sentence[i]) && (!ispunct(c_sentence[i]) || c_sentence[i] == '\'' || c_sentence[i] == '-')){
				temp += tolower(c_sentence[i]);
			}
			else if (isspace(c_sentence[i])) {
				replace_apostrophes(temp, s_words);
				s_words.push_back(temp);
				temp = "";
			}

			if (i == c_sentence.size() - 1) {
				replace_apostrophes(temp, s_words);
				s_words.push_back(temp);
				temp = "";
			}
		}

		//sort and output new sentence without duplicate words
		sort(s_words);
		for (int i = 0; i < s_words.size(); i++) {
			if (i == 0) {
				//cout << s_words[i] << endl;
				_ost << s_words[i] << endl;
			}
			if (i > 0 && s_words[i] != s_words[i - 1]) {
				//cout << s_words[i] << endl;
				_ost << s_words[i] << endl;
			}
		}
	}
}

int main()
try
{
	string iname = "input/p409_8_blind_text.txt";
	ifstream ist{ iname };

	string oname = "output/p409_8_blind_text_output.txt";
	ofstream ost{ oname };

	process(ist, ost);
	cout << "Done!" << endl;
	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
Done!
Please enter a character to exit
Input-File: p409_8_blind_text.txt
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.
Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.
In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.
Nullam dictum felis eu pede mollis pretium. Integer tincidunt.
Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.
Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim.
Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet.
Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui.
Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum.
Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus.
Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt.
Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
Output-File: p409_8_blind_text_output.txt
a
ac
adipiscing
aenean
aliquam
aliquet
amet
ante
arcu
augue
bibendum
blandit
commodo
condimentum
consectetuer
consequat
cras
cum
curabitur
cursus
dapibus
dictum
dis
dolor
donec
dui
duis
eget
eleifend
elementum
elit
enim
eros
et
etiam
eu
faucibus
felis
feugiat
fringilla
hendrerit
id
imperdiet
in
integer
ipsum
justo
laoreet
leo
libero
ligula
lorem
luctus
maecenas
magna
magnis
massa
mauris
metus
mollis
montes
mus
nam
nascetur
natoque
nec
neque
nibh
nisi
nulla
nullam
nunc
odio
orci
parturient
pede
pellentesque
penatibus
phasellus
porttitor
pretium
pulvinar
quam
quis
quisque
rhoncus
ridiculus
rutrum
sagittis
sapien
sed
sem
semper
sit
sociis
sodales
tellus
tempus
tincidunt
ullamcorper
ultricies
ut
varius
vel
velit
venenatis
vitae
vivamus
viverra
vulputate

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.