replace letters with strings

closed account (4i67ko23)
Hi, I want to replace a character (Example 'A') with a string (Example 'abc123')
Is this possible?
I've this code: But your= can only replace one character with another.
replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '1', 'Q' );
U can only do that sort of thing with dynamically allocated containers like vectors which can change size as u need using push_back and erase methods.

http://www.cplusplus.com/reference/stl/vector/push_back/
Last edited on
Read you previous thread. There are some some examples of how to do this. I copied my example here.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
std::string encode( const std::string &s )
{
	const char *from = "eo";   // substitute for your string literal
	const char *to[] = { "123", "456" };   // substitute for your string literals

	std::string t = s;
	
	for ( std::string::size_type pos = 0; pos < t.size(); /* */ )
	{
		pos = t.find_first_of( from, pos );
		if ( pos != std::string::npos )
		{
			std::string::size_type i = std::strchr( from, t[pos] ) - from;
			t.replace( pos, 1, to[i] );
			pos += std::strlen( to[i] );
		}
	}

	return ( t );
}
Last edited on
closed account (4i67ko23)
thanks! Now, how can I make an encode function?
I tried to switch *from and *to, but that didn't work...
I have not understood your question. I showed you encode function. What is the problem?
closed account (4i67ko23)
sorry, I mean 'decode'
Do you need a reverse operation?
closed account (4i67ko23)
yes, like that, how does it work?
It can do not work due to a possible ambiguity of an interpretation of codes. First of all show your codes for encode.
closed account (4i67ko23)
Well, this is my code now:
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
51
52
53
54
55
56
57
58
59
60
61
62
//--------------------------------------
#include <windows.h>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
//--------------------------------------
using namespace std;
//--------------------------------------
std::string encode( const std::string &s )
{
    const char *from = " 0123456789";   // substitute for your string literal
	const char *to[] = { "SPACE", "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"};   // substitute for your string literals

	std::string t = s;
	
	for ( std::string::size_type pos = 0; pos < t.size(); /* */ )
	{
		pos = t.find_first_of( from, pos );
		if ( pos != std::string::npos )
		{
			std::string::size_type i = std::strchr( from, t[pos] ) - from;
			t.replace( pos, 1, to[i] );
			pos += std::strlen( to[i] );
		}
	}

	return ( t );
}
//--------------------------------------
std::string decode( const std::string &s )
{

    const char *from = { "SPACE", "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"};   // substitute for your string literals
	const char *to[] = " 0123456789";   // substitute for your string literals

	std::string t = s;
	
	for ( std::string::size_type pos = 0; pos < t.size(); /* */ )
	{
		pos = t.find_first_of( from[], pos );
		if ( pos != std::string::npos )
		{
			std::string::size_type i = std::strchr( from[], t[pos] ) - from;
			t.replace( pos, 1, to[i] );
			pos += std::strlen( to[i] );
		}
	}

	return ( t );
}
//--------------------------------------
int main()
{
    string text_encode;
    text_encode = encode(" -0-1-2-3-4-5-6-7-8-9");
    cout << text_encode << endl;
    string text_decode;
    text_decode = decode(text_encode);
    cout << text_decode << endl;
    cin.get();
}


It's just to encode it and decode it back,,
Butt I get tons of errors
It is naturally that you get tons of errors because decode is writtten incorrectly.
You may not for example simply to write

1
2
    const char *from = { "SPACE", "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"};   // substitute for your string literals
	const char *to[] = " 0123456789";   // substitute for your string literals 


by analogy with my code

1
2
    const char *from = " 0123456789";   // substitute for your string literal
	const char *to[] = { "SPACE", "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"};   // substitute for your string literals 


because types of objects in your declarations are incorrect.

Function decode can not be writtten by means of simple copying of my function encode.
Last edited on
closed account (4i67ko23)
Well, how can I do it? Because otherwise the encode function is useless..
It is much more difficult task. I think you need a special structure as for example a tree that to perform searching of words in an encoded string.
if it is simply "find and replace" then I hope my code can help...

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
#include <iostream>
#include <string>
using namespace std;

//--------------------------------------
void replaceAll(string& s, const string& a, const string& b)
{
    //replace all a with b in s
    size_t pos = s.find(a);
    while (pos != string::npos)
    {
        s.replace(pos, a.length(), b);
        pos += b.length();
        pos = s.find(a, pos);
    }
}
//--------------------------------------
string cipher(const string& s, const string& command)
{
    const string from   = " 0123456789";
    const string to[11] = {"SPACE","ZERO","ONE","TWO","THREE","FOUR",
                           "FIVE","SIX","SEVEN","EIGHT","NINE"};
    string ret = s;

    if (command == "encode")
        for (int i = 0; i < from.length(); i++)
            replaceAll(ret, from.substr(i,1), to[i]);
    else if (command == "decode")
        for (int i = 0; i < from.length(); i++)
            replaceAll(ret, to[i], from.substr(i,1));
    
    return ret;
}
//--------------------------------------
int main()
{
    string textEncode;
    textEncode = cipher(" -0-1-2-3-4-5-6-7-8-9", "encode");
    cout << textEncode << endl;
    string textDecode;
    textDecode = cipher(textEncode, "decode");
    cout << textDecode << endl;
    cin.get();
    return 0;
}
Topic archived. No new replies allowed.