Replacing strings in a dynamic array

For my assignment I am required to replace certain words and phrases with other strings. I have looked at the <algorithm> library which I have trouble implicating into my assignment though, I also looked at http://www.java2s.com/Code/Cpp/String/stringreplace.htm which made sense, but since we are not allowed to use loops for our function and only in our main idk where to start. I used the library for the other 2/3 of this assignment to reverse and normalize. If someone could better explain how to use this parameter that would be awesome.

stage3.txt
1
2
3
4
5
6
7
7
Mayday mayday, wagon 6 bingo on juice
This is wagon 3, no toasters found
Apollo here, headed back to barn
Helo found the toasters, mark the time as stage 1
Decrypt stage 1 complete
Decrypt stage 2 complete


Need to replace:
"barn" to "Galactica"
"toaster" to "Cylon"
"juice" to "fuel"
"wagon" to "viper"
"stage 1" to the current time via <time.h>
"stage 2 to "FRAKKEN"

Replace entire string:
"Decrypt stage 1" to "Decrypted by" (then my name)
"Decrypt stage 2" to the current time via <time.h>

stage3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>
using namespace std;

string replaced(string s);

int main()
{
	int size = 0;
	string s;
	ifstream fin;
	ofstream fout;
	string result = "";

	fin.open("stage3.txt");
	fout.open("decrypted.txt");

	string* decryption;
	
	fin >> size;
	getline(fin, s);
	decryption = new string[size];
	for(int i = 0; i < size; i++)
	{
		getline(fin,s);
//call replaced() to find and replace
	}

    for(int i = 0; i < size; i++)
    {
    	fout << decryption[i] << endl;
    }
    fout << "Decryption complete" << endl;


   
	delete [] decryption;
	decryption = NULL;

	fout.close();
	return 0;
}

string replaced(string s)
{   
//use find() and replace(), no loops
	return s;
}
Last edited on
The problem with replace from algorithm is that string is a sequence of char. That means the iterator points to a singe char and hence you can replace only single char.


Use find/replace from string instead:

http://www.cplusplus.com/reference/string/string/find/
http://www.cplusplus.com/reference/string/string/replace/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <algorithm>
#include <string>

int main ()
{
    std::string words[] =
    {   "We", "all", "live", "in", "a", "yellow", "submarine", "\n",
        "yellow", "submarine,", "yellow", "submarine", "\n",
    };

    const std::size_t N = sizeof(words) / sizeof( words[0] ) ;

    for( const std::string& str : words ) std::cout << ' ' << str ;

    std::cout << "-----------------------------------\n" ;

    const std::string yellow = "yellow" ;
    const std::string lavender = "lavender" ;

    ///////////////////////////////////////////////////////////////////////////
    // replace strings in a sequence of strings: std::replace
    ///////////////////////////////////////////////////////////////////////////

    std::replace( words, words+N, yellow, lavender ) ;

    for( const std::string& str : words ) std::cout << ' ' << str ;


    std::cout << "-----------------------------------\n" ;

    std::string str = "We all live in a yellow submarine\n"
                       "yellow submarine, yellow submarine" ;
    std::cout << str << '\n' ;

    std::cout << "-----------------------------------\n" ;

    ///////////////////////////////////////////////////////////////////////////
    // replace substrings in a string: std::string::replace()
    ///////////////////////////////////////////////////////////////////////////

    for( std::size_t pos = str.find(yellow) ; pos != std::string::npos ; pos = str.find(yellow) )
        str.replace( pos, yellow.size(), lavender ) ;
    std::cout << str << '\n' ;

}

http://coliru.stacked-crooked.com/a/e5d4c200fb38618c
I have edited my post to get a more in depth answer, still unresolved.
try this:

create a function to replace a word.

1
2
3
4
5
6
7
8
9
string replace_with(const string&, const string&, const string&); //prototype

string replace_with(const string& word, const string& word_in_string,
         const string& dialogue)
{
    //string::pos_type variable will let you store the position
    // if the position is string::npos, then it isn't found.
    //return the modified (or unmodified) string
}


in your main function, you can use a for loop. That's all I will say for now. Hope this helps.
Topic archived. No new replies allowed.