Tuesday, June 8, 2010

Encoder and Decoder


using System;


using System.Collections.Generic;


using System.Linq;


using System.Text;


 


namespace Scores


{


    class Program


    {


        static void Main(string[] args)


        {


            string s = Console.ReadLine();


            if (!char.IsDigit(s[0]))//Encoder Section.


            {


                string s1 = "";


                string s2 = "";


                for (int i = 0; i < s.Length; i++)


                {


                    int ch = s[i];//Retreivimg the ASCII code of ith character.


                    s1 = s1 + ch.ToString();//COnctenating it into a string.


                }


                //Reversing the string of ASCII values.


                for (int i = s1.Length - 1; i >= 0; i--)


                {


                    s2 = s2 + s1[i];


                }


                Console.WriteLine(s2);


            }


            else//Decoder Section.


            {


                string s1 = "";


                string s2 = "";


                //Reversing the input encoded string.


                for (int j = s.Length - 1; j >= 0; j--)


                {


                    s1 = s1 + s[j];


                }


                int i = 0;


                while (i < s1.Length)


                {


                    int count = 0;


                    string s3 = "";


                    while (count < 2)//Taking two digits at a time.


                    {


                        s3 = s3 + s1[i];


                        i++;


                        count++;


                    }


                    count = int.Parse(s3);


                    if (count < 32)//Condition for taking three digits at a time.


                    {


                        s3 = s3 + s1[i];


                        i++;


                        count = int.Parse(s3);


                    }


                    s2 = s2 + char.ConvertFromUtf32(count);//Concatenating the character to the string.


                }


                Console.WriteLine(s2);


            }


        }


    }


}

No comments:

Post a Comment