Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

What is wrong with my C code? I've also added a screen shot with the expected outputs: #include railcipher.h #include #include #include #define MAX

What is wrong with my C code? I've also added a screen shot with the expected outputs: #include "railcipher.h"
#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; // Validation failed
char command[MAX_TEXT_LENGTH +8], result[MAX_TEXT_LENGTH];
printf("Enter command (encrypt/decrypt/quit): ");
while (fgets(command, sizeof(command), stdin)){
// Remove trailing newline
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;
}
void processCommand(const char *command, const int *key, int keyLength, char *result){
char text[MAX_TEXT_LENGTH];
if (sscanf(command, "encrypt %1023[^
]", text)==1){
encrypt(text, key, keyLength, result);
} else if (sscanf(command, "decrypt %1023[^
]", 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));
// Direction: 0= downward, 1= upward
int row =0, col =0, 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++]= rail[i][j];
}
}
strcpy(result, "Encrypted: Placeholder");
}
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));
int row =0, col =0, direction =0;
// First pass to mark the zigzag pattern
for (int i =0; i length; i++){
rail[row][col++]=1; // Just mark the cell as filled
if (row ==0) direction =0;
else if (row == keyLength -1) direction =1;
direction ? row-- : row++;
}
// Fill the rail matrix with the ciphertext
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++];
}
}
// Read off the matrix in zig-zag manner
memset(result,'\0', length +1); // Clear result
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++;
}
strcpy(result, "Decrypted: Placeholder");// Null-terminate the result
}
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]);
// Additional validation for the key elements can be added here
}
// Validation passed
return keyLength;
}
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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 2 Lnai 8725

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448505, 978-3662448502

More Books

Students also viewed these Databases questions

Question

1. Why do people tell lies on their CVs?

Answered: 1 week ago

Question

2. What is the difference between an embellishment and a lie?

Answered: 1 week ago