C++ Programming Blog

 
 
 

Image

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

 void encode_message(const char*);
 void decode_message(const char*);

 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(const char* 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(const char* 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;
    }

 
 
Didn't find what you were looking for? Find more on Program that will allow a spy to encode and decode messages