Question

I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key

My Current Code is:

void SubDecrypt(char *message, char *encryptKey) {
int iteration;
int iteration_Num_Two = 0;
int letter;
printf("Enter Encryption Key: \n");                                                           //Display the message to enter encryption key
scanf("%s", encryptKey);                                                                   //Input the Encryption key
for (iteration = 0; message[iteration] != '0'; iteration++)                               //loop will continue till message reaches to end
{
letter = message[iteration];                                                       //Assign the first character of message to letter
if (letter >= 'A' && letter <= 'Z')                                           //Check the letter is between 'A' to 'Z' i.e/ upper case character or not
{
letter = letter - 32;                                               //Move the letter to 32 characters back
}
if (letter >= 65 && letter <= 90) {                                   //After subtraction if the letter lies in the range 65 to 90(inclusive)
  
for (iteration_Num_Two = 0; iteration_Num_Two < 27; iteration_Num_Two++) //loop will continue from 0 to 26
{
if (message[iteration] == encryptKey[iteration_Num_Two])       //Check if the message[iteration] and encryptKey[iteration_Num_Two] will equal then terminate the loop
{
break;
}
}
message[iteration] = 'A' + iteration_Num_Two;                           //Set the message[iteration] to the value of iteration_Num_Two+'A'(uppercase) i.e/ 65+iteration_Num_Two
  
}
printf("%s\n", message);                                                           //Display the message

}
;
printf("CipherText message: %s\n", message);                                                   //Display the cipher text
printf("%s\n", encryptKey);                                                                   //Display the encryptKey
}

The Output is:

Please Enter the The Text You Wish to Encrypt/Decrypt: SCGGW Select option to be completed 1. Encrypt the entered mesaage wit

PLEASE BE ADVISED IVE ONLY PROVIDED MY CODE FOR THE FUNCTION NOT MY MAIN USER CODE. COULD YOU PLEASE SHOW ME WHERE IVE GONE WRONG

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You have made two or three mistakes , all these mistakes have been corrected in the below code

#include<stdio.h>

void SubDecrypt(char *message, char *encryptKey) {
int iteration;
int iteration_Num_Two = 0;
int letter;
printf("Enter Encryption Key: \n"); //Display the message to enter encryption key
scanf("%s", encryptKey); //Input the Encryption key
for (iteration = 0; message[iteration] != '\0'; iteration++) //loop will continue till message reaches to end
{
letter = message[iteration]; //Assign the first character of message to letter
if (letter >= 'a' && letter <= 'z') //Check the letter is between 'a' to 'z' i.e/ lower case character or not
{
letter = letter - 32; //Move the letter to 32 characters back for converting into upper case letter
}
if (letter >= 65 && letter <= 90) { //After subtraction if the letter lies in the range 65 to 90(inclusive)
  
for (iteration_Num_Two = 0; iteration_Num_Two < 27; iteration_Num_Two++) //loop will continue from 0 to 26
{
if (letter == encryptKey[iteration_Num_Two]) //Check if the letter and encryptKey[iteration_Num_Two] will equal then terminate the loop
{
break;
}
}
if(message[iteration]>='a' && message[iteration]<='z') //WHEN our original message is small so convert to equivalent small letter
message[iteration]='a'+iteration_Num_Two;
else
message[iteration] = 'A' + iteration_Num_Two; //Set the message[iteration] to the value of iteration_Num_Two+'A'(uppercase) i.e/ 65+iteration_Num_Two
  
}
printf("%s\n", message); //Display the message

}
;
printf("CipherText message: %s\n", message); //Display the cipher text
printf("%s\n", encryptKey); //Display the encryptKey
}

int main()
{
   char message[]="SCGGW";
   char encryptKey[100];
   SubDecrypt(message,encryptKey);
}

you can compare with your code to find your mistakes , also the key is in upper case that is why i've added the code where you need to first convert lower case to upper case ( you were doing for uppper case) .

Also in your output you have entered a wrong key which contain two consecutive W so remove that one W .

the correct output is shown below

PS D: \mingw-w64 1686-8.1.0-posix-dwarf-rt_v6-revo\mingw32\bin> ./a.exe Enter Encryption Key: KIMQCDHSOFAGUJWYERPLTZBXNV HCGG

I hope this will help you so please give positive ratings :))

Add a comment
Know the answer?
Add Answer to:
I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT