Inverse letters

I was just trying to pull a prank of my friend on facebook that we all get together and talk in a secret language. For that we thought of this one thing that everyletter would have a different letter value

example

A = z
B = y
C = x
D = w
E = v
F = u
G = t
H = s
I = r
J = q
K = p
L = o
M = n
N = m
O = l
P = k
Q = j
R = i
S = h
T = g
U = f
V = e
W = d
X = c
Y = b
Z = a

(The capitalization is not really important, but just to have a good prank)

i am getting confused that how would i read each letter of a sentence in using cin and then say like if 'A' then that letter == 'Z'.

or if 'v' then that letter == e

I have no idea in c++ coding just read a c++ for dummies once but dint really follow through. If anyone can give me tips or ideas, i will write the program.

Thanks, Cheers

Example -

Gsv Vztov rh mlg ivhklmwrmt gl rgh xzoo

should convert into

The Eagle is not responding to call
Last edited on
@Hasnain Attarwala

You could create 2 alphabet strings. One forward, a-z, and the other, z-a. Get the sentence from the user. Using a for loop, step through the first string, one at a time, til you find the first letter in the string input. Output the corresponding position of the first string, from the second. So, according to you example, you would check for the 'G', in lower case. The number would be 20. The 20th position in the second string is 'T'. That would be printed to screen. Then the next letter letter is checked in the original input, and so on, til the end of the input.
hmm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>

int main(void) {
	char ch;
	
	while (true)
	{
		std::cout << "Char? ";
		std::cin >> ch;
		ch -= ((ch - 'M') * 2) - 1;
			
		std::cout << ch << std::endl;
	}
	return 0;
}
}


Of course you've got to figure for lower-case.
Last edited on
You are looking at a basic substitution cipher, where each symbol (or letter of the alphabet) has an associated "code" symbol. The result is that all you need is a single lookup table to encode and decode the text.

Here are some basics. First, your lookup table:

1
2
#include <string>
string lookup_table = "zyxwvutsrqponmlkjihgfedcba";

Next, you need a way to change a letter into its code. For example, 'B' --> 'Y'.
There are three things to note:
  1. The table doesn't have any capital letters in it, so we'll have to pay attention to that.
  2. The letter 'B' is the second letter of the alphabet. The second item in the table is the letter 'Y'.
  3. The table only has letters in it. I'll mention more about this in a moment.

Now lets see our E and D functions:

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
#include <cctype>

char encode( char c )
  {
  // If it is not a letter, return it as-is. (see note 3)
  if (!isalpha( c )) return c;

  // We need to behave differently for capital and lowercase letters (see note 1)
  if (islower( c ))
    {
    // We get the index of the new letter in the table by subtracting the first letter of the alphabet. ('a' - 'a') == 0; ('b' - 'a') == 1; etc.
    return lookup_table[ c - 'a' ];
    }
  else
    {
    // Lookup the same way as above, but don't forget to make the result uppercase.
    return toupper( lookup_table[ c - 'A' ] );
    }
  }

char decode( char c )
  {
  // Same as above: letters only!
  if (!isalpha( c )) return c;

  // Going backwards, we find the letter in the table and then we know the
  // index into the alphabet of the original letter.

  if (islower( c ))
    {
    // (Remember, string::find() returns the index (0, 1, 2, ...) into the table of the argument.)
    return lookup_table.find( c ) + 'a';
    }
  else
    {
    return lookup_table.find( tolower( c ) ) + 'A';
    }
  }

Now, to convert an entire string, just apply a simple algorithm:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <algorithm>

string encode( string s )
  {
  transform( s.begin(), s.end(), (char (*)(char))encode );
  return s;
  }

string decode( string s )
  {
  transform( s.begin(), s.end(), (char (*)(char))decode );
  return s;
  }

To convert your messages back and forth is now simple.

1
2
3
4
5
string s = "The Eagle is not responding to call";
cout << encode( s ) << endl;

s = "Gsv Vztov rh MLD ivhklmwrmt gl rgh xzoo!";
cout << decode( s ) << endl;


Now, I mentioned above about non-letters in the table. It is entirely possible to have anything you want in the table, including spaces and punctuation and numbers. If you do this, though, you will have to make sure that there is a way for you to convert one to the other. The above examples were made very simple because we could just subtract/add the first letter of the alphabet.

If you put anything else in the table, though, you will have to do more than just a simple add and subtract. In this case, it might be worth having two tables: one of items that are not encoded, and one that matches. The E and D functions then become the same, only switching on which is the source and which is the lookup table.

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
string plaintext_table  = "abcdefghijklmnopqrstuvwxyz0123456789 +-!.?";
string ciphertext_table = "?.!-+ 9876543210zyxwvutsrqponmlkjihgfedcba";

// This function finds the character c in the "from" table
// and returns the corresponding character in the "to" table.
char encode_decode( char c, const string& from, const string& to )
  {
  if (islower( c ))
    {
    // Find the letter in the table.
    size_t n = from.find( c );

    // If it isn't there, then just return it as-is.
    if (n == string::npos) return c;

    // Else, translate it
    return to[ n ];
    }
  else
    {
    // This is the same as above, but watching out for case
    size_t n = from.find( tolower( c ) );

    if (n == string::npos) return c;

    return toupper( to[ n ] );
    }
  }

// Now the E and D functions differ only by which table is which.
char encode( char c )
  {
  encode_decode( c, plaintext_table, ciphertext_table );
  }

char decode( char c )
  {
  encode_decode( c, ciphertext_table, plaintext_table );
  }


One final note: you may observe that your ciphertext lookup table may have characters in any order you want. It doesn't need to be the same each time. The only important thing is that it only contain characters found in the plaintext table. This gives you and your friend the ability to change your cipher at any time. The only trick is that you'll have to get together to share the very first cipher. After that, you can send the new cipher through your encrypted conversation.

Keep in mind that substitution ciphers are very, very, very, very very very easy to break -- even for those new relatively to this stuff. They are fun to play with, but don't set up a business with it.

Hope this helps and have fun!
This is wrong, and correct in next posts.
Last edited on
#include <iostream>
using namespace std;
int main()
{
char a[1024];
int b,i=0;
cout<<"Please enter sentence which no more than 1024 bytes:"<<endl;
cin>>a;
for(i=0;i<1024&&a[i]!='\0';i++)
{
b=(int)a[i];
if(b>64&&b<91)
b=25-(b-65)+65;
if(b>96&&b<122)
b=25-(b-97)+97;
a[i]=(char)b;
}
cout<<a<<endl;
return 0;
}
use it like this:
Please enter sentence which no more than 1024 bytes:
Please,enter,sentence``````
Kovzhv,vmgvi,hvmgvmxv``````
This is wrong, and correct in next posts.


Duoas's post is not only correct, it's exceptionally correct. It's much more accurate than yours. I believe you may want to read the entire post before you say it is not right.

What happens when you enter a space into yours? Duoas has accounted for spaces, and also suggested the option to really mix up the cipher table to make it harder to break.
> Duoas's post is not only correct, it's exceptionally correct.

It is non-portable.

For instance, what would happen if you run the program on an IBM mainframe?
http://en.wikipedia.org/wiki/EBCDIC_037
Non-portability really doesn't make any sense in this context, because Hasnain Attarwala wants to know how to set up such a thing, not every detail about compatibility. And Duoas indeed answered his question exceptionally correct.
If you are programming on an EBCDIC system, you should already know better than to say c - 'A'. Use the two-table solution I posted instead.

I think issues with portability to arcane machines are a little beyond the OP ATM. Anyway, just how often do you think that people text their friends on Facebook using IBM mainframes?

Glad to see you are as belligerent to the participants at hand as usual, JL.
OMG! thanks so much, i dint realize that it would be so complicated.
Topic archived. No new replies allowed.