encode/decode

i would like to swap letters of the input message to a key that i have below and reverse for the decode

a=n
b=o
c=p
d=q
e=r
f=s
g=t
h=u
i=v
j=w
k=x
l=y
m=z



so if i put in "hi how are you"
it outputs
"uv ubj ner lbh"

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>

using namespace std;

int main()
{
	string code;
	string message;
	
	while (true)
	{

		cout << "encode or decode: ";
		cin >> code;

		if (code == "encode")
		{
			cout << "Input the message would u like to encode" << endl;
			getline(cin >> ws, message);
			cout << message << endl;
		}
		else if (code == "decode")
		{
			cout << "Input the message would u like to decode" << endl;
			cin >> message;
		}
		else
		{
			cout << "thats is none of the choices please try again" << endl;
			continue;
		}
		return 0;
	}
}
Have you used std::map before? You could map the keys to the a values. Another approach would be something like:

1
2
3
4
5
6
std::string alphabets[] = {"abc...", "nopq..."};

//encrypting:
    iterator over message
        message[i] = alphabets[1][i];


Though, you are shifting by 13 to the right each time. So you could even do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main()
{
    std::string message = "hi how are you";
    
    std::cout << "Message  : " << message << std::endl;
    
    std::transform(message.begin(), message.end(), message.begin(), [](int c){ if(isalpha(c) && (c=tolower(c)) && (c+= 13) && c > 'z') c -= 26; return c;});
    
    std::cout << "Encrypted: " << message << std::endl;
    
    std::transform(message.begin(), message.end(), message.begin(), [](int c){ if(isalpha(c) && (c=tolower(c)) && (c-= 13) && c < 'a') c += 26; return c;});
    
    std::cout << "Decrypted: " << message << std::endl;
}
Message  : hi how are you
Encrypted: uv ubj ner lbh
Decrypted: hi how are you
http://coliru.stacked-crooked.com/a/67774249fe4447e1

You can use functions/for loop instead of std::transform
i want to be able to input the message every time
i got it

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

using namespace std;

int main()
{
	string code;
	string message;
	
	while (true)
	{

		cout << "encode or decode: ";
		cin >> code;

		if (code == "encode")
		{
			cout << "Input the message would u like to encode" << endl;
			getline(cin >> ws, message);
			transform(message.begin(), message.end(), message.begin(), [](int c){ if (isalpha(c) && (c = tolower(c)) && (c += 13) && c > 'z') c -= 26; return c; });
			cout << message << endl;
		}
		else if (code == "decode")
		{
			cout << "Input the message would u like to decode" << endl;
			getline(cin >> ws, message);
			transform(message.begin(), message.end(), message.begin(), [](int c){ if (isalpha(c) && (c = tolower(c)) && (c -= 13) && c < 'a') c += 26; return c; });
			cout << message << endl;
		}
		else
		{
			cout << "Sorry, that is none of the choices please try again" << endl;
			continue;
		}
		return 0;
	}
}
Topic archived. No new replies allowed.