|
|
The aim of this program is to read text from a text file and then replace certain letters with others of the users choice and output the new text into a file. Currently the program does this by reading and replacing each letter in each string one at a time. I was wondering if there was a way to make it so i could for example replace all of the 'a' characters in the file at once without making the original text file one long string? currently the program is as follows: #include <iostream> #include<fstream> #include<string> #include<algorithm> using namespace std; int main () { string text; char replacementA, replacementB, replacementC; ifstream infile; ofstream outfile; infile.open("/Users/*********/Documents/jhhnkefkjnlf.txt"); outfile.open("/Users/*********/Documents/Myoutfile.txt"); if (!infile) { cout<<"unable to open file"<<endl; exit(1); } else { while(infile) { infile>>text; cout<<"Please enter the letter you would like to replace A with:"; cin>>replacementA; replace(text.begin(), text.end(), 'a', replacementA); replace(text.begin(), text.end(), 'A', replacementA); cout<<"Please enter the letter you would like to replace B with:"; cin>>replacementB; replace(text.begin(), text.end(), 'b', replacementB); replace(text.begin(), text.end(), 'B', replacementB); cout<<"Please enter the letter you would like to replace C with:"; cin>>replacementC; replace(text.begin(), text.end(), 'c', replacementC); replace(text.begin(), text.end(), 'C', replacementC); outfile<<text<<" "; } } return 0; } thanks, G |