Help reversing encryption code. Reverse what its done.

Sup guys. First year c++ here. Still learning and ive taken it upon myself to go through old daily programmer assignments to see if i can understand and modify them with what ive learned.

The encryption code here reads through your input and whenever it comes to a consonant (vowels are exempt) it double it and puts a 'o' between them. For example: input = Boat would output Bob-o-a-tot (without the dashes). I see how things work but now im having trouble seeing how to reverse the effect to create a decryption, using c++. Any help would be amazing!

So im trying to delete the two letters that follow a consonant.

#include <iostream>

using namespace std;

int main()
{
string SAMPLE;
getline(cin, SAMPLE);
string output = "";


for(int i = 0; i < SAMPLE.length(); i++)
{
char XletterX =(SAMPLE.at(i));
if((XletterX == 'a')||(XletterX == 'e')||(XletterX == 'i')||(XletterX == 'o')
||(XletterX == 'u')||(XletterX == 'y')||(XletterX == 'A')||(XletterX == 'E')||(XletterX == 'I')||(XletterX == 'O')||(XletterX == 'U')||(XletterX == 'Y'))
{
output += XletterX;
} else {
output += XletterX;
output.append("o");
output += XletterX;
}
}
cout << output << endl;;
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
#include <iostream>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

const string consonant {"bBcCdDfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXyYzZ"};

int main()
{
    cout << "Enter string: \n";
    string input;
    getline (cin, input);
    int len = input.size();
    for(int i = 0; i < len; i++)
    {
        if(consonant.find(input[i])!= string::npos)
        {
            for (int j = i + 1; j < len; j++)
            {
                input [j] = input [j + 1];
            }
            len --;
            for (int j = i + 1; j < len; j++)
            {
                input [j] = input [j + 1];
            }
            len --;
        }
    }
    cout << input;


}
Last edited on
I could hug you. Thank you so much! now i can continue working on my project. Hopefully i can finish this weekend. It should be a fun encryption project
I do have a few questions as to how it works. If you dont mind?

int len = input.size();
for(int i = 0; i < len; i++)
{
if(consonant.find(input[i])!= string::npos)
{
for (int j = i + 1; j < len; j++)
{
input [j] = input [j + 1];
}
len --;
for (int j = i + 1; j < len; j++)
{
input [j] = input [j + 1];
}
len --;
}
}

having issues with what this is doing exactly. Some things are foreign to me as of now
The program works on the principle of deleting an element from a data structure (such as std::string) at a specified position, applied twice here for the 'o' and the consonant's repeat.

So when we've found a consonant (line 18 in my program) we copy the elements of the array towards the left once (22), removing the 'o', decrement the size of the array (24), copy the elements towards left once more (27), removing that consonant's repeat, and again decrement (29)

Btw, you can remove the #include<map> from the program, it has stayed back from an interim approach
Topic archived. No new replies allowed.