Problem 3 The ABRACADABRA language has just five lowercase l

Problem 3 The ABRACADABRA language has just five lowercase letters a, b, c,d,r, and every combination of these letters (but no other letters) is a word in the ABRACADABRA language. For example, abba is a word in the language, but aBBa and abbe are not. The good people of ABRACADABRA often en- crypt their words, so that they can send each other messages which their adversaries cannot understand. To this end, they use the following simple encryption process, called a substitution cipher. First consider the natural (or lexicographic) order of the five letters, where a is the first letter, b is the second letter, and so on, with r being the last letter. Two parties who wish to communicate with each other agree in advance on a a secret order of the five letters in the language, also called the encryptio key (for example r,b, c, d, a). To encrypt a message, they create a table consisting of two rows. The first row lists the letters a,b, c, d,rin their natural order, while the second row lists these same letters in the secret order of the encryption key. Here is an example of such a table: natural order a b C d r encryption key r b c d a An arbitrary word in the ABRACADABRA language can be now encrypted by replacing every letter in the word by an encrypted version thereof that appears in the second row of the table. For example if the encryption key is rbcda as above, every a is replaced by r and every r is replaced by a, while the letters b, c, d remain unchanged. Thus the word abba gets encrypted rbbr. Warm up exercises: If the secret key is abcdr and the source word is abbaca, what is the encrypted word? If the secret key is arcdb and the source word is abba car, what is the encrypted word?

Solution

program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
   char ab_word[7];
   char key[5];
   char enc_word[7];
   char key_value[]={\'a\',\'b\',\'c\',\'d\',\'r\'};
   int i,j,key_count,a_count=0,b_count=0,c_count=0,d_count=0,r_count=0;
   int flag=0;
   clrscr();
   printf(\"Enter key:\\t\");
   gets(key);
   for(i=0;i<5;i++)
   {
       flag=0;
       if(key[i]==\' \')
       {
           printf(\"No space allowed in the key\");
           getch();
           exit(0);

       }

       if(a_count>1 || b_count >1 || c_count >1 ||d_count>1 || r_count>1)
       {
           printf(\"the key is not valid\");
           exit(0);
       }
       for(j=0;j<5;j++)
       {
           if(key[i]==key_value[j])
           {
               flag=1;

               if(key[i]==\'a\')
               a_count+=1;
           if(key[i]==\'b\')
               b_count+=1;
           if(key[i]==\'c\')
               c_count+=1;
           if(key[i]==\'d\')
               d_count+=1;
           if(key[i]==\'r\')
               r_count+=1;

           key_count+=1;
           }
       }
       if(flag==0)
       {
           printf(\"the key is not valid\");
           exit(0);
       }
   }
   printf(\"Enter word:\\t\");
   fgets(ab_word,sizeof(ab_word)+1,stdin);


   for(i=0;i<7;i++)
       {
           flag=0;
           for(j=0;j<5;j++)
           {
               if(ab_word[i]==key_value[j])
               {
                   flag=1;
               }
           }

           if(flag!=1)
           {
               printf(\"You did not speak in ABRACADABRA to me!\");
               exit(0);
           }

          }

   for(j=0;j<7;j++)
   {
       if(ab_word[j]==\'a\')
       {
           enc_word[j]=\'r\';
       }
       else if(ab_word[j]==\'r\')
       {
           enc_word[j]=\'a\';

       }
       else
       {
           enc_word[j]=ab_word[j];
       }
   }
       printf(\"Encrypted word:\\t\");
       for(j=0;j<7;j++)
           printf(\"%c\",enc_word[j]);

   getch();
}

 Problem 3 The ABRACADABRA language has just five lowercase letters a, b, c,d,r, and every combination of these letters (but no other letters) is a word in the
 Problem 3 The ABRACADABRA language has just five lowercase letters a, b, c,d,r, and every combination of these letters (but no other letters) is a word in the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site