#include <map> #include <sstream> #include <fstream> #include <iostream> #include <string > #include <exception>using namespace std;ifstream& openfile (ifstream &in,const string &filename) {in.close ();//close in case it is alrady openin.clear ();//clear any existing Errorsin.open (FILENAME.C_STR ());//open the The file we were Given//in is either bound to the specified files, or is in an error condition state of return in;//condition is good if open succeeded}int main (int argc,char* * argv) {map<string,string> trans_map;string key,value;ifstream map_file;if (!openfile (Map_file, "transform.txt ") {throw Runtime_error (" No Transformation File ");} Read the Tansformation map and build the Mapwhile (map_file>>key>>value) {Trans_map.insert (pair<string ,string> (Key,value));} Ifstream input;if (!openfile (Input, "Source.txt")) {throw runtime_error ("No input File");} String Line;//hold each line from the Input//read the text to transform it a line at a timewhile (Getline (input,line)) {ISTR Ingstream StReam (line); Read the line a word at a timestring word;//reads the string stream bool Linefirst=true;//controls whether a space is printedwhile (STREAM&G T;>word) {//ok:the actual mapwork,this part is the heart of the Programmap<string,string>::const_iterator iter= Trans_map.find (Word), if (Iter!=trans_map.end ()) {//replace It by the transformation value in the Mapword=iter->second ;} if (Linefirst) {cout<<word;linefirst=false;} else{cout<< "" <<word;//print space between words;} Cout<<endl;//done with the line of Input}return 0;}
Transform.txt File Contents:
EM themcuz Becausegratz gratefuli inah Nopossupposedsez said Tanx Thankswuz was
Source.txt File Contents:
Nah i sez tanx cuz i wuz pos tonot cuz i wuz Gratz
Program Run output:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Word conversion Map Object