Decryption project

Decryption stage 3:

The structure of this program is similar to stage 2; you will open the file “stage3.txt” and read the sentence into a string.

You will send the string to a function called “replace_sentence”:

string replace_sentence(string s);

This function will replace certain words in the string with other words. You will then return the modified string.

You must use string functions.

USE IF-STATEMENTS TO DO THE SUBSTITUTIONS. DO NOT USE A SWITCH-STATEMENT IN THIS FUNCTION!!!

Here are the words that need to be replaced. Each may appear one time in each string.

Replace “barn” with “Galactica”
Replace “toaster” with “Cylon”

You must replace the entire string if you find these phrases

After you finish write out the string to a new file named “decrypted.txt”.

I already made stage3.txt

i dont know how to replace a word in the function and call it into my program can someone help me.

#include<iostream>
#include<fstream>
#include<string>
#include<stdio.h>
#include<ctype.h>
using namespace std;

string reverse_sentence(string s);
string normalize_sentence(string s);
string replace_sentence(string s);

int main ()
{
ifstream code;
string text;
code.open("one_message.txt");
getline(code,text);
text = reverse_sentence(text);
cout << text << endl;
ofstream ofs("stage2.txt");
if(!ofs)
{
cout<< "error opening file for output" << endl;
return -1;
}
ofs << text << endl;
ofs.close();

ifstream code2;
string text2;
code2.open("stage2.txt");
getline(code2, text2);
text2 = normalize_sentence(text2);
cout << text2 << endl;
ofstream ofs2("stage3.txt");
if(!ofs2)
{
cout << "error opening file for output" << endl;
return -1;
}
ofs << text2 << endl;
ofs.close();

ifstream code3;
string text3;
code3.open("stage3.txt");
getline(code3, text3);
text3 = replace_sentence(text3);
cout << text3 << endl;
ofstream ofs3("decrypted.txt");
if(!ofs3)
{
cout << "error opening file for output" << endl;
return -1;
}
ofs << text 3 << endl;
ofs.close();
return 0;

}

string reverse_sentence(string s)
{
s = string (s.rbegin(),s.rend());
return s;
}
string normalize_sentence(string s)
{
for(int i=0; i<s.size();i++)
{
s[i] = tolower(s[i]);
}
return s;
}
string replace_sentence(string s)
{
if((s.find(barn, 0)))
{
s.replace(Galatica, barn.size);
Galatica++;
}
}

follow this , i think its works fine
1
2
3
4
5
6
    int found = str.find("barn");                 
								
	if ( found != string::npos )			
	{
		str.replace(found, number of chars replaced ,Galactica  ) ;                         
	}


http://www.cplusplus.com/reference/string/string/replace/
Topic archived. No new replies allowed.