Replace function return + question

closed account (4i67ko23)
Hi there!
This is my code for replacing automatic characters. But I don't now how to do 'return <???>;'
I learn c++ from this site;
http://www.icer.nl/tutorials/programmeren/cplusplus/functies.php
It's dutch. This is my first work with functions, and I don't get what's wrong...
Then I've another question, can I replace one character with multiple? Like replace an 'A' with 'ABC'?
My source:
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;
int encode(string DECODE_TEXT)
{ 
    string ENCODE_TEXT = DECODE_TEXT;
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), ' ', 'L' );//"1" -->"UIH"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '1', 'Q' );//"1" -->"UIH"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '2', 'W' );//"2" -->"YEH"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '3', 'E' );//"3" -->"OUJ"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '4', 'R' );//"4" -->"PLO"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '5', 'T' );//"5" -->"QWC"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '6', 'Y' );//"6" -->"RFD"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '7', 'U' );//"7" -->"BHM"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '8', 'I' );//"8" -->"POW"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '9', 'O' );//"9" -->"KMJ"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'a', 'P' );//"a" -->"AWE"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'b', '0' );//"b" -->"ASD"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'c', 'S' );//"c" -->"QBC"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'd', 'D' );//"d" -->"OPL"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'e', 'F' );//"e" -->"EYB"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'f', 'G' );//"f" -->"UHY"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'g', 'H' );//"g" -->"KCB"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'h', 'J' );//"h" -->"UHJ"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'i', 'K' );//"i" -->"KIU"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'j', 'M' );//"j" -->"QTR"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'k', 'Z' );//"k" -->"UVG"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'l', 'X' );//"l" -->"UYE"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'm', 'C' );//"m" -->"MAT"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'n', 'V' );//"n" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'o', 'B' );//"o" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'p', 'N' );//"p" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'q', 'A' );//"q" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'r', '1' );//"r" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 's', '8' );//"s" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 't', '0' );//"t" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'u', '9' );//"u" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'v', '5' );//"v" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'w', '4' );//"w" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'x', '3' );//"x" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'y', '6' );//"y" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'z', '2' );//"z" -->"/"
    return ENCODE_TEXT;
}

int main()
{
    system("cls");
    string input;
    getline (cin, input);
    encode(input);

    string ENCODE_TEXT
    cout << ENCODE_TEXT << endl;
    cin.get();
    main();
    return 0;
}

An error:
58 C:\Users\Mathijs\Documents\C++\test14.cpp `ENCODE_TEXT' does not name a type
Last edited on
at first the encode function returns a string so you have to declare it like string encode() ect....

in main function where you declared the ENCODE_TEXT there is no " ; " sign.

sorry for my english
closed account (4i67ko23)
Crap...
Your english is well, always those small things, I don't get what the error mean.
Thanks for help and welcome to the forum!
Do you also now how to replace an 'A' with multiple characters? Like 'A' --> 'ABC'?
Last edited on
closed account (4i67ko23)
I still have an error,,

C:\Users\Mathijs\Documents\C++\test14.cpp In function `int encode(std::string)':

47 C:\Users\Mathijs\Documents\C++\test14.cpp cannot convert `std::string' to `int' in return

C:\Users\Mathijs\Documents\C++\test14.cpp In function `int main()':

56 C:\Users\Mathijs\Documents\C++\test14.cpp `ENCODE_TEXT' undeclared (first use this function)


Source:

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
#include <windows.h>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int encode(string DECODE_TEXT)
{ 
    string ENCODE_TEXT = DECODE_TEXT;
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), ' ', 'L' );//"1" -->"UIH"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '1', 'Q' );//"1" -->"UIH"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '2', 'W' );//"2" -->"YEH"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '3', 'E' );//"3" -->"OUJ"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '4', 'R' );//"4" -->"PLO"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '5', 'T' );//"5" -->"QWC"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '6', 'Y' );//"6" -->"RFD"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '7', 'U' );//"7" -->"BHM"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '8', 'I' );//"8" -->"POW"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '9', 'O' );//"9" -->"KMJ"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'a', 'P' );//"a" -->"AWE"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'b', '0' );//"b" -->"ASD"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'c', 'S' );//"c" -->"QBC"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'd', 'D' );//"d" -->"OPL"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'e', 'F' );//"e" -->"EYB"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'f', 'G' );//"f" -->"UHY"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'g', 'H' );//"g" -->"KCB"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'h', 'J' );//"h" -->"UHJ"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'i', 'K' );//"i" -->"KIU"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'j', 'M' );//"j" -->"QTR"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'k', 'Z' );//"k" -->"UVG"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'l', 'X' );//"l" -->"UYE"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'm', 'C' );//"m" -->"MAT"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'n', 'V' );//"n" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'o', 'B' );//"o" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'p', 'N' );//"p" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'q', 'A' );//"q" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'r', '1' );//"r" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 's', '8' );//"s" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 't', '0' );//"t" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'u', '9' );//"u" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'v', '5' );//"v" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'w', '4' );//"w" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'x', '3' );//"x" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'y', '6' );//"y" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'z', '2' );//"z" -->"/"
    return ENCODE_TEXT;
}

int main()
{
    system("cls");
    string input;
    getline (cin, input);
    encode(input);
    cout << ENCODE_TEXT << endl;
    cin.get();
    main();
    return 0;
}
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;
string encode(string DECODE_TEXT)
{
    string ENCODE_TEXT = DECODE_TEXT;
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), ' ', 'L' );//"1" -->"UIH"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '1', 'Q' );//"1" -->"UIH"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '2', 'W' );//"2" -->"YEH"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '3', 'E' );//"3" -->"OUJ"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '4', 'R' );//"4" -->"PLO"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '5', 'T' );//"5" -->"QWC"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '6', 'Y' );//"6" -->"RFD"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '7', 'U' );//"7" -->"BHM"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '8', 'I' );//"8" -->"POW"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), '9', 'O' );//"9" -->"KMJ"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'a', 'P' );//"a" -->"AWE"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'b', '0' );//"b" -->"ASD"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'c', 'S' );//"c" -->"QBC"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'd', 'D' );//"d" -->"OPL"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'e', 'F' );//"e" -->"EYB"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'f', 'G' );//"f" -->"UHY"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'g', 'H' );//"g" -->"KCB"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'h', 'J' );//"h" -->"UHJ"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'i', 'K' );//"i" -->"KIU"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'j', 'M' );//"j" -->"QTR"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'k', 'Z' );//"k" -->"UVG"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'l', 'X' );//"l" -->"UYE"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'm', 'C' );//"m" -->"MAT"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'n', 'V' );//"n" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'o', 'B' );//"o" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'p', 'N' );//"p" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'q', 'A' );//"q" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'r', '1' );//"r" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 's', '8' );//"s" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 't', '0' );//"t" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'u', '9' );//"u" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'v', '5' );//"v" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'w', '4' );//"w" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'x', '3' );//"x" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'y', '6' );//"y" -->"/"
    replace( ENCODE_TEXT.begin(), ENCODE_TEXT.end(), 'z', '2' );//"z" -->"/"
    return ENCODE_TEXT;
}

int main()
{
    system("cls");
    string input;
    getline (cin, input);
    encode(input);

    string ENCODE_TEXT;
    cout << ENCODE_TEXT << endl;
    cin.get();
    main();
    return 0;
}
closed account (4i67ko23)
It doesn't replace anything :(
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    system("cls");
    string input;
    getline (cin, input);

    cout << encode(input) << endl;
    cin.get();
    main();
    return 0;
}
you forgot a semi-colon, among other things.

change the return type of encode() to string, then

 
string ENCODE_TEXT = encode(input);


whoops, sorry, was quite late and didn't refresh page
Last edited on
closed account (4i67ko23)
Thanks all, it works now!
Only the par that replace one character with multiple I don't get, any help? :)
You need no header <windows.h>.

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
#include <algorithm>
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

string encode( const string &s )
{
   const char from[] = " 123456789abcdefghijklmnopqrstuvwxyz"
   const char to[]     = "LQWERTYUIOP0SDFGHJKMZXCVBNAI80954362";

   string t = s;

   for ( size_t i = 0; i < sizeof( decode ) - 1; i++ )
   {
      replace( t.begin(), t.end(), from[i], to[i[ );
   }

   return ( t );
}

int main()
{
    system( "cls" );

    string input;

    getline( cin, input );

    string ENCODE_TEXT = encode( input );

    cout << ENCODE_TEXT << endl;
    cin.get();

    return 0;
}
Last edited on
@Mathijs001
Only the par that replace one character with multiple I don't get, any help? :)


It is a more difficult task. You should combine member functions of class std::string with standard C functions. The function could look for example the following way

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
> can I replace one character with multiple? Like replace an 'A' with 'ABC'?

This is what I suggest:
1
2
3
4
5
6
7
8
a. Create (one-time) a look up table which maps a char to its replacement string ( 'A' => "ABC" etc. )

b. Create an empty string to hold the encoded text.

c. Iterate through the chars in the original string one by one
       Look up the char in the look up table
             if found: append the replacement string to the encoded text
             else:  append the char to the encoded text


For example (C++11):
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
std::string replace_char_with_multiple( const std::string& original_text )
{
    static const std::map<char,std::string> look_up =
    {
          { '1', "UIH" }, // "1" -->"UIH"
          { '2', "YEH" },

          // etc...

          { 'y', "/" },
          { 'z', "/" }
    };
    static const auto end = look_up.end() ;

    std::string encoded_text ;

    for( char c : original_text )
    {
        auto iter = look_up.find(c) ;
        if( iter != end ) encoded_text += iter->second ;
        else encoded_text += c ;
    }

    return encoded_text ;
}
Topic archived. No new replies allowed.