Answered step by step
Verified Expert Solution
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 MAXTEXTLENGTH
#define MAXKEYLENGTH
int mainint argc, char argv
int keyMAXKEYLENGTH;
int keyLength validateKeyargc argv, key;
if keyLength
return ;
char commandMAXTEXTLENGTH resultMAXTEXTLENGTH;
printfEnter command encryptdecryptquit: ;
while fgetscommand sizeofcommand stdin
commandstrcspncommand;
if strncmpcommand "quit",
break;
else
processCommandcommand key, keyLength, result;
printfs result;
printfEnter command encryptdecryptquit: ;
return ;
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 ;
return keyLength;
void processCommandconst char command const int key int keyLength, char result
char textMAXTEXTLENGTH;
if sscanfcommand "encrypt s text
encrypttext key, keyLength, result;
else if sscanfcommand "decrypt s 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;
int row col ;
int 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 toupperrailij; Convert to uppercase
resultidx; Nullterminate the result string
void decryptconst char text const int key int keyLength, char result
int length strlentext;
char railkeyLengthlength;
memsetrail sizeofrail;
railcipher:
STDIN:
Test Case #:
encrypt hello world!
quit
CLAs: rai.lcipher
STDOUT: Help please write in C Create a railiphers 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 #:
encrypt hello world!
decrypt HWELLLORDO
quit
STDOUT:
railcsipher: Fatal error! The encryption key length cannot exceed
STDIN:
Test Case #:
encrypt Attack postponed until two AM
decrypt TTNAAPTMTSUOAODWCGOIXKNLYPET.Z
quit
STDOUT:
TTNAAPTMTSUOAODWCOIXKNLYPETZ
ATTACKPOSTPONEDUNTILTWOAMXYZ
int row col ;
int direction ;
for int i ; i length; i
railrowcol;
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 && idx length
railij textidx;
memsetresult length ;
row col idx ;
direction ;
for int i ; i length; i
resultidx railrowcol;
if row direction ;
else if row keyLength direction ;
direction row : row;
resultidx;
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