Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Numerical AnalysisRSS Feeds

Program that will allow a spy to encode and decode messages

Posted By: Easy Tutor     Category: C++ Programming     Views: 5928

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.

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.

Code for Program that will allow a spy to encode and decode messages in C++ Programming

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <ctype.h>
 # include <conio.h>

 void encode_message(constchar*);
 void decode_message(constchar*);

 int main( )
    {
       clrscr( );

       fstream File("CP-16.txt",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<"\n Unable to open the input file."<<endl;
         cout<<"\n Press any key to exit.";

         getch( );
         exit(EXIT_FAILURE);
      }

       char Data[100]={NULL};

       do
      {
         strset(Data,NULL);

         File.getline(Data,80);

         if(strcmp(Data,NULL)==0)
        break;

         if(isdigit(Data[0]))
        decode_message(Data);

         else
        encode_message(Data);
      }
       while(1);

       File.close( );

       getch( );
       return 0;
    }


 /*************************************************************************///--------------------------  encode_message( )  ------------------------///*************************************************************************/void encode_message(constchar* String)
    {
       char Message[300]={NULL};

       int length=strlen(String);
       int ascii_code;

       char Ascii_code[10]={NULL};

       for(int i=0;i<length;i++)
      {
         ascii_code=int(String[i]);

         strset(Ascii_code,NULL);

         itoa(ascii_code,Ascii_code,10);
         strcat(Message,Ascii_code);
      }

       strrev(Message);

       cout<<Message<<endl;
    }

 /*************************************************************************///--------------------------  decode_message( )  ------------------------///*************************************************************************/void decode_message(constchar* Number)
    {
       char Message[300]={NULL};

       strcpy(Message,Number);
       strrev(Message);

       int length=strlen(Message);

       char Ascii_code[10]={NULL};

       for(int i=0;i<length;i++)
      {
         strset(Ascii_code,NULL);

         Ascii_code[0]=Message[i];
         i++;
         Ascii_code[1]=Message[i];

         if(Ascii_code[0]=='1')
        {
           i++;
           Ascii_code[2]=Message[i];
        }

         cout<<char(atoi(Ascii_code));
      }

       cout<<endl;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program that will allow a spy to encode and decode messages is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!