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

Program language is C. The offset value to be entered is -37. Should display lea

ID: 3788120 • Letter: P

Question

Program language is C.

The offset value to be entered is -37. Should display lear a yellow shirt on *eb.

The printf only prints out the lear part but terminates at the whitespace after lear. I need the whole statement to printout as mentioned above. Need help printing out the statement lear a yellow shirt on *eb. What is wrong?

#include<stdio.h>
#include<string.h>
int main()
{
   int len=0;
   int i=0;
   char ch;
   int index,k;
  
   char Ctext[100]="WPL2KLK9PWWZ7K3ST24KZYKfPMKJ4SKLYOKRP4KFKP842LK0ZTY43";
   char key[63]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
  
   len=strlen(Ctext);
   char text[len];
   printf("please enter a key for decrypt: ");
   scanf("%d",&k);
   for(i=0;i<len;i++)
   {
       ch=Ctext[i];
       switch(ch)
       {
           case 'a':index=0;
           break;
           case 'b':index=1;
           break;
           case 'c':index=2;
           break;
           case 'd':index=3;
           break;
           case 'e':index=4;
           break;
           case 'f':index=5;
           break;
           case 'g':index=6;
           break;
           case 'h':index=7;
           break;
           case 'i':index=8;
           break;
           case 'j':index=9;
           break;
           case 'k':index=10;
           break;
           case 'l':index=11;
           break;
           case 'm':index=12;
           break;
           case 'n':index=13;
           break;
           case 'o':index=14;
           break;
          
          
           case 'p':index=15;
           break;
           case 'q':index=16;
           break;
           case 'r':index=17;
           break;
           case 's':index=18;
           break;
           case 't':index=19;
           break;
           case 'u':index=20;
           break;
           case 'v':index=21;
           break;
           case 'w':index=22;
           break;
           case 'x':index=23;
           break;
           case 'y':index=24;
           break;
           case 'z':index=25;
           break;
           case 'A':index=26;
           break;
           case 'B':index=27;
           break;
           case 'C':index=28;
           break;
           case 'D':index=29;
           break;
           case 'E':index=30;
           break;
           case 'F':index=31;
           break;
           case 'G':index=32;
           break;
           case 'H':index=33;
           break;
           case 'I':index=34;
           break;
           case 'J':index=35;
           break;
           case 'K':index=36;
           break;
           case 'L':index=37;
           break;
           case 'M':index=38;
           break;
           case 'N':index=39;
           break;
           case 'O':index=40;
           break;
           case 'P':index=41;
           break;
           case 'Q':index=42;
           break;
           case 'R':index=43;
           break;
           case 'S':index=44;
           break;
           case 'T':index=45;
           break;
           case 'U':index=46;
           break;
           case 'V':index=47;
           break;
           case 'W':index=48;
           break;
           case 'X':index=49;
           break;
           case 'Y':index=50;
           break;
           case 'Z':index=51;
           break;
           case '0':index=52;
           break;
           case '1':index=53;
           break;
           case '2':index=54;
           break;
           case '3':index=55;
           break;
           case '4':index=56;
           break;
           case '5':index=57;
           break;
           case '6':index=58;
           break;
           case '7':index=59;
           break;
           case '8':index=60;
           break;
           case '9':index=61;
           break;
          
      
      
   }
   text[i]=key[(k+index)%63];

}
   printf("decrypted text: %s",text);

return 0;

}

Explanation / Answer

It is not working after SPACE because the k+index gives negative value and there are no negative indices in an array.

When the loop encounters "K" in cipher text, the value (k+index) gives a negative term; in this case 36+(-37)=-1, which is not an index.

So, we need to map the negative numbers to the 0-63. We achieve this by replacing
text[i]=key[(k+index)%63];

with the following code:

int p=(k+index);
if(p<0.0)
p=p+63;
text[i]=key[(p)%63];

which gives us the output:
decrypted text: lear a yellow shirt on Feb 9th and get 5 extra points