Message encoder malfunction

I am attempting to make a program that will encode (or scramble) a message that can be decoded by the same program.
I am entering a string which will have every character swapped out for a different character.

The problem is, the scramble [void thing()] is not properly scrambling the characters.

abcdefghijklmnopqrstuvwxyz becomes kavcchcnhbwjndhflnfqkpfvfd
when it should be uaxegicmobtjrzhslnwqkpyvfd

Maybe I've just done everything wrong from the start and replace() isn't the proper function for what I am trying to accomplish!

Either way, I would greatly appreciate any input!

Thanks!

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  //Message encoder and decoder
//Jon Gabay | CasualProgrammerG
#include <algorithm>
#include <string>
#include <iostream>

void thing()
{
    std::cout << "Please enter the message that you wish to encode..." << std::endl;

    std::string s;
    std::getline(std::cin, s); //gets a string. Unlike cin, getline will grab more than one word, so it will include spaces

    std::replace(s.begin(), s.end(), 'A', 'U'); // replace all 'A' to 'U'
    std::replace(s.begin(), s.end(), 'B', 'A'); // replace all 'B' to 'A'
    std::replace(s.begin(), s.end(), 'C', 'X'); // replace all 'C' to 'X'
    std::replace(s.begin(), s.end(), 'D', 'E'); // replace all 'D' to 'E'
    std::replace(s.begin(), s.end(), 'E', 'G'); // and so on...
    std::replace(s.begin(), s.end(), 'F', 'I');
    std::replace(s.begin(), s.end(), 'G', 'C');
    std::replace(s.begin(), s.end(), 'H', 'M');
    std::replace(s.begin(), s.end(), 'I', 'O');
    std::replace(s.begin(), s.end(), 'J', 'B');
    std::replace(s.begin(), s.end(), 'K', 'T');
    std::replace(s.begin(), s.end(), 'L', 'J');
    std::replace(s.begin(), s.end(), 'M', 'R');
    std::replace(s.begin(), s.end(), 'N', 'Z');
    std::replace(s.begin(), s.end(), 'O', 'H');
    std::replace(s.begin(), s.end(), 'P', 'S');
    std::replace(s.begin(), s.end(), 'Q', 'L');
    std::replace(s.begin(), s.end(), 'R', 'N');
    std::replace(s.begin(), s.end(), 'S', 'W');
    std::replace(s.begin(), s.end(), 'T', 'Q');
    std::replace(s.begin(), s.end(), 'U', 'K');
    std::replace(s.begin(), s.end(), 'V', 'P');
    std::replace(s.begin(), s.end(), 'W', 'Y');
    std::replace(s.begin(), s.end(), 'X', 'V');
    std::replace(s.begin(), s.end(), 'Y', 'F');
    std::replace(s.begin(), s.end(), 'Z', 'D');

    std::replace(s.begin(), s.end(), 'a', 'u'); // it IS case-sensitive, so you need another set for lower case characters
    std::replace(s.begin(), s.end(), 'b', 'a');
    std::replace(s.begin(), s.end(), 'c', 'x');
    std::replace(s.begin(), s.end(), 'd', 'e');
    std::replace(s.begin(), s.end(), 'e', 'g');
    std::replace(s.begin(), s.end(), 'f', 'i');
    std::replace(s.begin(), s.end(), 'g', 'c');
    std::replace(s.begin(), s.end(), 'h', 'm');
    std::replace(s.begin(), s.end(), 'i', 'o');
    std::replace(s.begin(), s.end(), 'j', 'b');
    std::replace(s.begin(), s.end(), 'k', 't');
    std::replace(s.begin(), s.end(), 'l', 'j');
    std::replace(s.begin(), s.end(), 'm', 'r');
    std::replace(s.begin(), s.end(), 'n', 'z');
    std::replace(s.begin(), s.end(), 'o', 'h');
    std::replace(s.begin(), s.end(), 'p', 's');
    std::replace(s.begin(), s.end(), 'q', 'l');
    std::replace(s.begin(), s.end(), 'r', 'n');
    std::replace(s.begin(), s.end(), 's', 'w');
    std::replace(s.begin(), s.end(), 't', 'q');
    std::replace(s.begin(), s.end(), 'u', 'k');
    std::replace(s.begin(), s.end(), 'v', 'p');
    std::replace(s.begin(), s.end(), 'w', 'y');
    std::replace(s.begin(), s.end(), 'x', 'v');
    std::replace(s.begin(), s.end(), 'y', 'f');
    std::replace(s.begin(), s.end(), 'z', 'd');
    std::cout << s << std::endl;
    /*
    Message does not scramble properly.
    abcdefghijklmnopqrstuvwxyz should equal uaxegicmobtjrzhslnwqkpyvfd

    abcdefghijklmnopqrstuvwxyz instead equals kavcchcnhbwjndhflnfqkpfvfd
    */
}

void antithing()
{
    std::string p;
    std::getline(std::cin, p); //getline for the already scrambled message that you would like decoded

    std::replace(p.begin(), p.end(), 'U', 'A'); // replace all 'U' to 'A'
    std::replace(p.begin(), p.end(), 'A', 'B'); // replace all 'A' to 'B'
    std::replace(p.begin(), p.end(), 'X', 'C'); // replace all 'X' to 'C'
    std::replace(p.begin(), p.end(), 'E', 'D'); // replace all 'E' to 'D'
    std::replace(p.begin(), p.end(), 'G', 'E'); // and so on...
    std::replace(p.begin(), p.end(), 'I', 'F');
    std::replace(p.begin(), p.end(), 'C', 'G');
    std::replace(p.begin(), p.end(), 'M', 'H');
    std::replace(p.begin(), p.end(), 'O', 'I');
    std::replace(p.begin(), p.end(), 'B', 'J');
    std::replace(p.begin(), p.end(), 'T', 'K');
    std::replace(p.begin(), p.end(), 'J', 'L');
    std::replace(p.begin(), p.end(), 'R', 'M');
    std::replace(p.begin(), p.end(), 'Z', 'N');
    std::replace(p.begin(), p.end(), 'H', 'O');
    std::replace(p.begin(), p.end(), 'S', 'P');
    std::replace(p.begin(), p.end(), 'L', 'Q');
    std::replace(p.begin(), p.end(), 'N', 'R');
    std::replace(p.begin(), p.end(), 'W', 'S');
    std::replace(p.begin(), p.end(), 'Q', 'T');
    std::replace(p.begin(), p.end(), 'K', 'U');
    std::replace(p.begin(), p.end(), 'P', 'V');
    std::replace(p.begin(), p.end(), 'Y', 'W');
    std::replace(p.begin(), p.end(), 'V', 'X');
    std::replace(p.begin(), p.end(), 'F', 'Y');
    std::replace(p.begin(), p.end(), 'D', 'Z');

    std::replace(p.begin(), p.end(), 'u', 'a'); // it IS case-sensitive, so you need another set for lower case characters
    std::replace(p.begin(), p.end(), 'a', 'b');
    std::replace(p.begin(), p.end(), 'x', 'c');
    std::replace(p.begin(), p.end(), 'e', 'd');
    std::replace(p.begin(), p.end(), 'g', 'e');
    std::replace(p.begin(), p.end(), 'i', 'f');
    std::replace(p.begin(), p.end(), 'c', 'g');
    std::replace(p.begin(), p.end(), 'm', 'h');
    std::replace(p.begin(), p.end(), 'o', 'i');
    std::replace(p.begin(), p.end(), 'b', 'j');
    std::replace(p.begin(), p.end(), 't', 'k');
    std::replace(p.begin(), p.end(), 'j', 'l');
    std::replace(p.begin(), p.end(), 'r', 'm');
    std::replace(p.begin(), p.end(), 'z', 'n');
    std::replace(p.begin(), p.end(), 'h', 'o');
    std::replace(p.begin(), p.end(), 's', 'p');
    std::replace(p.begin(), p.end(), 'l', 'q');
    std::replace(p.begin(), p.end(), 'n', 'r');
    std::replace(p.begin(), p.end(), 'w', 's');
    std::replace(p.begin(), p.end(), 'q', 't');
    std::replace(p.begin(), p.end(), 'k', 'u');
    std::replace(p.begin(), p.end(), 'p', 'v');
    std::replace(p.begin(), p.end(), 'y', 'w');
    std::replace(p.begin(), p.end(), 'v', 'x');
    std::replace(p.begin(), p.end(), 'f', 'y');
    std::replace(p.begin(), p.end(), 'd', 'z');
    std::cout << p << std::endl;
}

int main()
{
    std::cout << "Welcome to Message scrambler!\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n[1] scramble \n[2] descramble" << std::endl;
    int i;
    std::cin >> i; //menu input

    if (i == 1)
    {
        std::cin.ignore();
        thing(); //runs void thing();
        return 0;
    }
    else if (i == 2)
    {
        std::cin.ignore();
        antithing(); //runs void antithing();
        return 0;
    }
    else
    {
        return 0;
    }
}
The statements in the function are getting run in sequential order, so the way you have it now, you may end up changing a character more than once.

If I put in Hello - the function runs through all the upper case level statements. H is first changed into M, then M changes into R, then R into N.

1
2
3
   std::replace(s.begin(), s.end(), 'H', 'M');
    std::replace(s.begin(), s.end(), 'M', 'R');
    std::replace(s.begin(), s.end(), 'R', 'N');


You want to make sure you're not revisiting a character you've already encrypted.


Edit:
I might look into using some container like a map for the letter substitution values. http://www.cplusplus.com/reference/map/map/
Last edited on
Topic archived. No new replies allowed.