Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//** Use C and use only use **// The ABRACADABRA language has just five lowercas

ID: 3638866 • Letter: #

Question

//** Use C and use only use **//

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 encrypt 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 encryption 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,r in 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 abbacar, what is the encrypted word?

Write a C program that encrypts words in the ABRACADABRA language. The program should prompt the user to enter a secret key and a word. You can assume that the secret key will consist of the five characters a,b,c,d,r listed in some order, each appearing exactly once, with no spaces. The leftmost letter is the _rst letter of the secret key (hence it should replacethe letter a in your encryption), and so on. The word should be a valid word in the ABRACADABRA language, consisting of seven letters. If the user enters anything after the seven letters, you should ignore this input. On the other hand, if the first seven characters typed in by the user do not constitute a valid word in the ABRACADABRA language, the program should print an appropriate error message and terminate (remember to verify the success of the input functions scanf() and/or getchar()). The output of the program should be the encrypted word. Here are two sample runs of this program, assuming that the executable file is named abracadabra encoder.

1) /home/userXYZ/ECE15/Lab2> abracadabra encoder
Enter key: rbcda
Enter word: abbacar
Encrypted word: rbbrcra
2) /home/userXYZ/ECE15/Lab2> abracadabra encoder
Enter key: rdbca
Enter word: acrAbar
You did not speak in ABRACADABRA to me!

Explanation / Answer

please rate - thanks

#include<stdio.h>
#include<conio.h>
int main()
{int max=7;
char input[max+1],letters[]={'a','b','c','d','r'};
char output[max+1],key[6];
int i,j,k,used[5],bad;
do
{
for(i=0;i<6;i++)
      used[i]=0;
bad=0;
printf("Enter key ");
gets(key);
for(i=0;i<6;i++)
    {for(j=0;j<6;j++)
         {if(key[i]==letters[j])
              used[j]++;
         }
    }
for(i=0;i<6;i++)
     if(used[i]!=1)
         {bad=1;
          printf("invalid key-start over ");
          i=10;
          }
}while(bad!=0);
printf("Enter word ");
gets(input);
bad=0;
for(i=0;i<max;i++)
    for(j=0;j<5;j++)
         if(input[i]==key[j])
               {output[i]=letters[j];
                bad++;
                }
output[max]='';
if(bad==max)
    printf("Encrypted word: %s ",output);
else
    printf("You did not speak in ABRACADABRA to me! ");
        
getch();
return 0;
}