reverse chars of string in text file

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

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

#include "std_lib_facilities.h"

void reverseWord(string& w) {
	string temp;
	for (int i = 1; i < w.size() + 1; i++) {
		temp += w[w.size() - i];
	}
	w = temp;
}

void readWriteFile() {
	string iname = "input/p409_12_blind_text.txt";
	ifstream ifs{ iname };
	string oname = "output/p409_12_blind_text_out.txt";
	ofstream ofs{ oname };

	while (true) {
		if (ifs.eof()) {
			break;
		}
		string temp;
		string tempReverse;
		char ch;
		if (getline(ifs,temp)) {
			cout << temp << endl;
			string word;
			for (int i = 0; i < temp.size(); i++) {
				if (isalpha(temp[i])) {
					word += temp[i];
				}
				else {
					if (word.size() != 0) {
						reverseWord(word);
						tempReverse += word;
						word = "";
					}
					tempReverse += temp[i];
				}
				if (i == temp.size() - 1) {
					tempReverse += '\n';
				}
			}
		}
		ofs << tempReverse;		
	}
}

int main()
try
{
	readWriteFile();
	keep_window_open();
}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open();
}
catch (...) {
	cout << "Exiting" << endl;
	keep_window_open();
}
Output:
Please enter a character to exit
Input-File: p409_12_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_12_blind_text_out.txt
meroL muspi rolod tis tema, reutetcesnoc gnicsipida tile. naeneA odommoc alugil tege rolod.
naeneA assam. muC siicos euqotan subitanep te singam sid tneirutrap setnom, rutecsan sulucidir sum.
cenoD mauq silef, seicirtlu cen, euqsetnellep ue, muiterp siuq, mes. alluN tauqesnoc assam siuq mine.
cenoD edep otsuj, allignirf lev, teuqila cen, etatupluv tege, ucra.
nI mine otsuj, sucnohr tu, teidrepmi a, sitanenev eativ, otsuj.
malluN mutcid silef ue edep sillom muiterp. regetnI tnudicnit.
sarC subipad. sumaviV mutnemele repmes isin. naeneA etatupluv dnefiele sullet.
naeneA oel alugil, rotittrop ue, tauqesnoc eativ, dnefiele ca, mine.
mauqilA merol etna, subipad ni, arreviv siuq, taiguef a, sullet. sullesahP arreviv allun tu sutem suirav teeroal.
euqsiuQ murtur. naeneA teidrepmi. maitE seicirtlu isin lev eugua. rutibaruC reprocmallu seicirtlu isin. maN tege iud.
maitE sucnohr. saneceaM supmet, sullet tege mutnemidnoc sucnohr, mes mauq repmes orebil, tis tema gnicsipida mes euqen des muspi.
maN mauq cnun, tidnalb lev, sutcul ranivlup, tirerdneh di, merol. saneceaM cen oido te etna tnudicnit supmet.
cenoD eativ neipas tu orebil sitanenev subicuaf. malluN siuq etna. maitE tis tema icro tege sore subicuaf tnudicnit.
siuD oel. deS allignirf siruam tis tema hbin. cenoD selados sittigas angam. deS tauqesnoc, oel tege mudnebib selados, eugua tilev susruc cnun,

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.