Answered step by step
Verified Expert Solution
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 MAXTEXTLENGTH #define MAXKEYLENGTH int mainint argc, char argv int keyMAXKEYLENGTH; int keyLength validateKeyargc argv, key; if keyLength return ; Validation failed char commandMAXTEXTLENGTH resultMAXTEXTLENGTH; printfEnter command encryptdecryptquit: ; while fgetscommand sizeofcommand stdin Remove trailing newline commandstrcspncommand ; if strncmpcommand "quit", break; else processCommandcommand key, keyLength, result; printfs result; printfEnter command encryptdecryptquit: ; return ; void processCommandconst char command const int key int keyLength, char result char textMAXTEXTLENGTH; if sscanfcommand "encrypt text encrypttext key, keyLength, result; else if sscanfcommand "decrypt text decrypttext key, keyLength, result; else strcpyresult "Invalid command. Please enter 'encrypt', 'decrypt', or 'quit'."; void encryptconst char text const int key int keyLength, char result int length strlentext; char railkeyLengthlength; memsetrail sizeofrail; Direction: downward, upward int row col direction ; for int i ; i length; i railrowcol texti; if row direction ; else if row keyLength direction ; direction row : row; int idx ; for int i ; i keyLength; i for int j ; j length; j if railij resultidx railij; strcpyresult "Encrypted: Placeholder"; void decryptconst char text const int key int keyLength, char result int length strlentext; char railkeyLengthlength; memsetrail sizeofrail; int row col direction ; First pass to mark the zigzag pattern for int i ; i length; i railrowcol; Just mark the cell as filled if row direction ; else if row keyLength direction ; direction row : row; Fill the rail matrix with the ciphertext int idx ; for int i ; i keyLength; i for int j ; j length; j if railij && idx length railij textidx; Read off the matrix in zigzag manner memsetresult length ; Clear result row col idx ; direction ; for int i ; i length; i resultidx railrowcol; if row direction ; else if row keyLength direction ; direction row : row; strcpyresult "Decrypted: Placeholder"; Nullterminate the result int validateKeyint argc, char argv int key if argc printfUsage: s argv; return ; int keyLength atoiargv; if keyLength keyLength MAXKEYLENGTH printfError: Invalid key length. ; return ; for int i ; i keyLength; i keyi atoiargvi ; Additional validation for the key elements can be added here Validation passed return keyLength;
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 MAXTEXTLENGTH
#define MAXKEYLENGTH
int mainint argc, char argv
int keyMAXKEYLENGTH;
int keyLength validateKeyargc argv, key;
if keyLength return ; Validation failed
char commandMAXTEXTLENGTH resultMAXTEXTLENGTH;
printfEnter command encryptdecryptquit: ;
while fgetscommand sizeofcommand stdin
Remove trailing newline
commandstrcspncommand
;
if strncmpcommand "quit",
break;
else
processCommandcommand key, keyLength, result;
printfs
result;
printfEnter command encryptdecryptquit: ;
return ;
void processCommandconst char command const int key int keyLength, char result
char textMAXTEXTLENGTH;
if sscanfcommand "encrypt
text
encrypttext key, keyLength, result;
else if sscanfcommand "decrypt
text
decrypttext key, keyLength, result;
else
strcpyresult "Invalid command. Please enter 'encrypt', 'decrypt', or 'quit'.";
void encryptconst char text const int key int keyLength, char result
int length strlentext;
char railkeyLengthlength;
memsetrail sizeofrail;
Direction: downward, upward
int row col direction ;
for int i ; i length; i
railrowcol texti;
if row direction ;
else if row keyLength direction ;
direction row : row;
int idx ;
for int i ; i keyLength; i
for int j ; j length; j
if railij resultidx railij;
strcpyresult "Encrypted: Placeholder";
void decryptconst char text const int key int keyLength, char result
int length strlentext;
char railkeyLengthlength;
memsetrail sizeofrail;
int row col direction ;
First pass to mark the zigzag pattern
for int i ; i length; i
railrowcol; Just mark the cell as filled
if row direction ;
else if row keyLength direction ;
direction row : row;
Fill the rail matrix with the ciphertext
int idx ;
for int i ; i keyLength; i
for int j ; j length; j
if railij && idx length railij textidx;
Read off the matrix in zigzag manner
memsetresult length ; Clear result
row col idx ;
direction ;
for int i ; i length; i
resultidx railrowcol;
if row direction ;
else if row keyLength direction ;
direction row : row;
strcpyresult "Decrypted: Placeholder"; Nullterminate the result
int validateKeyint argc, char argv int key
if argc
printfUsage: s
argv;
return ;
int keyLength atoiargv;
if keyLength keyLength MAXKEYLENGTH
printfError: Invalid key length.
;
return ;
for int i ; i keyLength; i
keyi atoiargvi ;
Additional validation for the key elements can be added here
Validation passed
return keyLength;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started