Encoder-Decoder

Being in charge of the computer department of the Agency of International Espionage, you are asked to write a program that will allow a spy to encode and decode their messages.

You can assume a spy’s message is at most 80 characters long, and it includes all the upper and lowercase letters of the alphabet plus the space, and any of the following characters:

!   ,   .   :   ;   ?

The following is an ASCII table of the valid characters in a message:

“A”         65           “a”          97           “ “           32

“B”         66           “b”         98            “!”           33

.                               .                           “,”           44

.                               .                           “.”           46

.                               .                           “:”           58

“Y”          89           “y”          121         “;”           59

“Z”          90           “z”          122         “?”          63

The algorithm that you should use to encode messages is to take the ASCII value of each character in the message, starting with the last character in the message and ending with the first character in the message. You should then add on to the coded message this ASCII value written in reverse order. For example, if the ASCII value is 123, the encoded message should contain the string “321”. There should be no spaces separating the numbers in the encoded message.

Input and Output:

The input file consists of one or more lines with a normal (not encoded) or encoded message each. Output file must have the same number of lines with the corresponding encoded message or the decoded one, respectively.

Sample Input

abc

798999

Have a Nice Day !

Sample Output

998979

cba

332312179862310199501872379231018117927

Press any key to continue . . .

 

Here is the C# implementation of Encoder-Decoder:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace EncoderDecoder

{

    class EncoderDecoder

    {

        static void Main(string[] args)

        {

            try

            {

                //Reading the contents from C:\EncoderDecoder.txt

                StreamReader sr = new StreamReader(@"C:\EncoderDecoder.txt");

                for (string str = sr.ReadLine(); str != null; str = sr.ReadLine())

                {

                    //Checking if the string needs to be encoded

                    if (!char.IsDigit(str[0]))

                    {

                        //This string needs to be encoded.

                        for (int i = str.Length – 1; i >= 0; i–)

                        {

                            Console.Write(Reverse(Encoder(str[i]).ToString()));

                        }

                        Console.WriteLine();

                    }

                    else

                    {

                        //This string needs to be decoded.

                        for (int i = str.Length – 1; i >= 0;)

                        {

                            //Logic to differentiate between 2 digits and 3 digits ASCII code

                            if (int.Parse(str[i].ToString()) != 1)

                            {

                                Console.Write(Decoder(int.Parse(Reverse(str.Substring(i – 1, 2)))));

                                i = i – 2;

                            }

                            else

                            {

                                Console.Write(Decoder(int.Parse(Reverse(str.Substring(i – 2, 3)))));

                                i = i – 3;

                            }

                        }

                        Console.WriteLine();

                    }

                }

                sr.Close();

            }

            catch (Exception e)

            {

                Console.WriteLine(e.Message);

            }

        }

        public static int Encoder(char ch)

        {

            //Returns the ASCII value of character

            return ch;

        }

        public static string Reverse(string str)

        {

            //Reverse the number and pass it as a string

            string tempStr = "";

            for (int i = str.Length – 1; i >= 0; i–)

            {

                tempStr = tempStr + str[i];

            }

            return tempStr;

        }

        public static char Decoder(int num)

        {

            //Returns the character for a given ASCII value

            return char.Parse(char.ConvertFromUtf32(num));

        }

    }

}

 

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s