Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help, please! What's wrong with my C code? It isn't working. Can you help me with the decryption and encryption logic? The screen shot at

Help, please! What's wrong with my C code? It isn't working. Can you help me with the decryption and encryption logic? The screen shot at the end is what the 'Expected Outcome' should be once you compile and run the code (the cipher text as shown in the screen shot is supposed to be converted to uppercase): #include "railcipher.h"
#include
#include
#include
#include
#define MAX_TEXT_LENGTH 1024
#define MAX_KEY_LENGTH 26
int main(int argc, char *argv[]){
int key[MAX_KEY_LENGTH];
int keyLength = validateKey(argc, argv, key);
if (keyLength ==-1)
return 1;
char command[MAX_TEXT_LENGTH +8], result[MAX_TEXT_LENGTH];
printf("Enter command (encrypt/decrypt/quit): ");
while (fgets(command, sizeof(command), stdin)){
command[strcspn(command,"")]=0;
if (strncmp(command, "quit", 4)==0){
break;
} else {
processCommand(command, key, keyLength, result);
printf("%s", result);
}
printf("Enter command (encrypt/decrypt/quit): ");
}
return 0;
}
int validateKey(int argc, char *argv[], int *key){
if (argc 3){
printf("Usage: %s
", argv[0]);
return -1;
}
int keyLength = atoi(argv[1]);
if (keyLength 1|| keyLength > MAX_KEY_LENGTH){
printf("Error: Invalid key length.
");
return -1;
}
for (int i =0; i keyLength; i++){
key[i]= atoi(argv[i +2]);
}
return keyLength;
}
void processCommand(const char *command, const int *key, int keyLength, char *result){
char text[MAX_TEXT_LENGTH];
if (sscanf(command, "encrypt %1023s", text)==1){
encrypt(text, key, keyLength, result);
} else if (sscanf(command, "decrypt %1023s", text)==1){
decrypt(text, key, keyLength, result);
} else {
strcpy(result, "Invalid command. Please enter 'encrypt', 'decrypt', or 'quit'.");
}
}
void encrypt(const char *text, const int *key, int keyLength, char *result){
int length = strlen(text);
char rail[keyLength][length];
memset(rail,'\0', sizeof(rail));
int row =0, col =0;
int direction =0;
for (int i =0; i length; ++i){
rail[row][col++]= text[i];
if (row ==0) direction =0;
else if (row == keyLength -1) direction =1;
direction ? row-- : row++;
}
int idx =0;
for (int i =0; i keyLength; ++i){
for (int j =0; j length; ++j){
if (rail[i][j]!='\0'){
result[idx++]= toupper(rail[i][j]); // Convert to uppercase
}
}
}
result[idx]='\0'; // Null-terminate the result string
}
void decrypt(const char *text, const int *key, int keyLength, char *result){
int length = strlen(text);
char rail[keyLength][length];
memset(rail,'\0', sizeof(rail));
-./01railcipher:
STDIN: [
-Test Case #0:
encrypt hello world!
quit
]
CLAs: [./01rai.lcipher 4.5-12]
STDOUT: [Help, please write in C. Create a railipher.s, railcipher. h, and makefile.
railcipher: Fatal error! First CLA must be a valid integer.
]
STDIN: [
decrypt HWELLLORDO
quit
]
STDOUT: [
railcyipher: Fatal error! The encryption key passed as CLAs is not valid.
]
STDIN: [
--Test Case #2:
encrypt hello world!
decrypt HWELLLORDO
quit
]
STDOUT: [
railcsipher: Fatal error! The encryption key length cannot exceed 26.
]
STDIN: [
--Test Case #3:
encrypt Attack postponed until two A.M.
decrypt T.TNAAPTMTSUOAODWCGOIXKNLYPET.Z
quit
]
STDOUT: [
TTNAAPTMTSUOAODWCOIXKNLYPETZ
ATTACKPOSTPONEDUNTILTWOAMXYZ
]
int row =0, col =0;
int direction =0;
for (int i =0; i length; ++i){
rail[row][col++]=1;
if (row ==0){
direction =0;
} else if (row == keyLength -1){
direction =1;
}
direction ? row-- : row++;
}
int idx =0;
for (int i =0; i keyLength; ++i){
for (int j =0; j length; ++j){
if (rail[i][j]==1 && idx length){
rail[i][j]= text[idx++];
}
}
}
memset(result,0, length +1);
row = col = idx =0;
direction =0;
for (int i =0; i length; ++i){
result[idx++]= rail[row][col++];
if (row ==0) direction =0;
else if (row == keyLength -1) direction =1;
direction ? row-- : row++;
}
result[idx]='\0';
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

an element of formality in the workplace between different levels;

Answered: 1 week ago