Simple encryption program

Write a simple encryption program using string functions which apply the substitution method. Explain the program by parts. Here is the given substitution table:
Substitution table:
A = *
E = $
I = /
O = +
U = -

EXAMPLE:
Enter Message here: Meet me now
Encrypted Message: M$$t m$ n+w
I hope you'll be able to appreciate the work I've put into this.
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
import std.ascii;
import std.algorithm;
import std.stdio;

void main()
{
	immutable auto substitutionTable = [
		'a': '*',
		'e': '$',
		'i': '/',
		'o': '+',
		'u': '-'
	];

	char[] text;

	write("Enter your text: ");
	readln(text);

	foreach (ref c; text)
		if (canFind(substitutionTable.keys, c.toLower))
			c = substitutionTable[cast(char)c.toLower];

	writeln(text);
}
thanks can i ask how about using strings ?
Here's a C++ translation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <map>
#include <cctype>
#include <string>

const std::map<char, char> m = {
    {'a', '*'}, {'e', '$'}, {'i', '/'}, {'o', '+'}, {'u', '-'}
};

int main()
{
    std::cout << "Enter Message here: ";
    std::string s;
    getline(std::cin, s);

    for(auto& c: s) {
         auto i = m.find(std::tolower(c));
         if(i != m.end())
             c = i->second;
    }

    std::cout << "Encrypted Message: " << s << '\n';
}

online demo (modified for gcc 4.5 compatibility): http://ideone.com/UTDdJ
Last edited on
Topic archived. No new replies allowed.