CSE220 Homework 3
CSE220 Homework 3
Learning Outcomes
After completion of this homework assignment you should be able to:
● Perform non-trivial string processing in C.
● Some basic understanding of C pointers
IMPORTANT:
● Y ou will be provided strPtr.c for Part 1, and caesar.c for part 2. Only these files
will be used by the TAs to mark your code and implementation. Do Not include
the main() function [the driver function] in these files. Use a separate file for this.
Then include/call the defined functions through pre-processors.
● Include a text document if you feel the need to explain the scope of your work.
● Use comments to explain your implementation to a person who is looking at your
code for the first time. There will be credits for comments.
● No time extension will be given.
● Do not submit your code on Brightspace. It will not be graded.
Part 1:
se of C string lib support is not allowed! You have to make it your own.
U
Use the standard array approach to implement the functions.
Test Cases
● int strgLen( s ):Return the length of the strings
ample Input
S eturn Value
R
“Stony Brook” 11
“CSE 220” 7
“C” 1
“System Fundamental” 18
“1” 1
“”/empty string 0
NULL -1
● void strgCopy( s, d ): copy the content of strings (source) to d (destination).
● v
oid strgChangeCase( s ): for each character in thestring, if it is an alphabet, reverse
the case of the character (upper to lower, and lower to upper). Keep the non-alphabet
characters as is.
ontent of s (before calling the
C ontent of s (after calling the
C
function) function)
“Stony Brook” “sTONY bROOK”
“CSE220” “cse220”
“C” “c”
“System Fundamental” “sYSTEM fUNDAMENTAL”
“1” “1”
“”/empty “”/empty
NULL Do nothing and return from the
function
● int strgDiff( s1, s2 ):compare strings s1 and s2.Return the index where the first
difference occurs. Return -1 if the two strings are equal.
ontent of s1
C Content of s2 eturn Value
R
“Hello” “Hello” -1
“CSE-220” “CSE220” 3
“CSE220” “SE220” 0
“” “” 0
Note: In case any of s1 or s2 is NULL, return -2
● v
oid strgInterleave( s1, s2, d ):copy s1 and s2 tod, interleaving the characters of s1
and s2. If one string is longer than the other, after interleaving, copy the rest of the
longer string to d. For example, given s1 = “abc” and s2 = “123”, then d = “a1b2c3”. If s1
= “abcdef” and s2 = “123”, then d = “a1b2c3def”
Note: In case any of s1, s2, or d is NULL, do nothing and return from the function. If the
does not have enough space, interleave until you reach the last byte of d and null-terminate
d
it. For example, give s1 = “abc” and s2 = “123”, and give d has only 5 bytes, after calling the
function, d should be = “a1b2”
Part 2:
Overview
he Caesar Cipher is one of the oldest methods to convert data into a format unauthorized
T
users cannot recognize. Encryption is a process of converting data into secret code, and
decryption is the exact opposite process, i.e., converting code to original data. It shifts each
letter a few positions right to encrypt it. Further, this can be shifted to the left to get the
message i.e to decrypt it (Caesar Cipher).
aesargaveaverysimplemethodtoencryptanddecrypttheinformation.Itisalsoknownas
C
shiftCaesarasthemethodshiftsthecharacterkeypositionsahead.Ifthecurrentcharacteris
a and the key = 4, then Cipher text will store e, i.e., 4 positions ahead of a. To decrypt the
same,wego4positionsbehindfore,whichgivesusaback.Thesameistrueforthedigits.If
the current digit character is 1 and the key = 4, then Cipher text will store 5.Todecryptthe
same, we go 4 positions back from5to get1.
orthisassignment,theuppercaseletterwillbereplacedwithuppercaseletterwhenshifted.
F
Forexample,“A”willbereplacedwith“C”whenshiftedrightfourstepsusingKey=2. Similarly,
the lowercase letter will be replaced with lower case letter and did will be replaced with a digit.
questionmayariseabouttheextremecharacterssuchasyorYandthekey=3.Wearenot
A
leftwithalphabets,soitstartscountingagainwithborB.Similarly,iftheencryptedmessage
is on decrypting, we get y or Y with the same key value. The same logic is applied to the
digits.Fordigit9andthekey=3,wewillhave2.Wewillget9withkey=3bymovingbackfrom
2 during the decryption process.
In this assignment, you will write a C program for the Caesar Cipher encryption. Files caesar.c
and caesar.h will be provided to you that will contain the interfaces that you need to implement.
Part A: Encryption
int encrypt( const char *plaintext, char *ciphertext, int key)
omplete the function encrypt, which encrypts a message with caesar cypher strategy using a
C
given key.
Arguments:
● p laintext: a null-terminated string to be encrypted. The string contains only ASCII
characters
● ciphertext: a null-terminated string used to encrypt the plaintext according to the caesar
c ipher approach. A mutable character array is passed as ciphertext so that its contents
may be changed.
Return Value:
T
● he number of characters from plaintext successfully encoded, or
● -1 if there was insufficient memory to encode the EOM marker (__EOM__). Note:
ciphertext will have some text/string which defines its capacity. If this capacity is not
enough. One example is if the ciphertext is “” or “abc”. In both cases, no enough
memory for the marker
● -2 if any of the plaintext or ciphertext is NULL.
c iphertext can be an empty string or it might contain text. Characters in ciphertext may be
modified to encode characters or the EOM marker. (The EOM marker is a 7 character string
“__EOM__”)
Example:
Part B: Decryption
int decrypt(const char *ciphertext, char *plaintext, int key)
Arguments:
c iphertext: a null-terminated string that encodes a message using cipher.
●
● plaintext: a null-terminated string of (possibly) random characters. A mutable character
array is passed as plaintext so that its contents may be changed.
he function must null-terminate the plaintext message. If the ciphertext contains more than
T
one EOM marker, decrypt the message up to the first EOM marker. If the plaintext string
cannot store all of the decrypted ciphertext, store the first strlen(plaintext) characters of the
encrypted ciphertext in plaintext.
Returns:
● the number of encrypted characters successfully decrypted, or one of the following error
codes:
○ 0 if the length of the plaintext string is 0 (e.g., as computed by strlen). This means
we cannot decrypt any portion of the ciphertext.
○ -1 if the ciphertext is missing the EOM marker. This means the ciphertext is
invalid.
○ -2 if any of the ciphertext or plaintext is NULL.
● When any error is present in the arguments (meaning that the function is going to return
an error code), the function must not change the contents of the plaintext.
● C
heck for the error cases in the order provided. i.e., if both errors are present, return the
maximum (i.e., least negative) valid error code.
Characters in plaintext may be modified only to decrypt the ciphertext.
Test Cases:
Part A: Encryption
int encrypt( const char *plaintext, char *ciphertext, int key)
Part B: Decryption
int decrypt(const char *ciphertext, char *plaintext, int key)
Ciphertext Test Cases
he provided test cases are nowhere near comprehensive. It is your responsibility to write
T
additional test cases to verify the correctness of your code. If you create new test cases using
Criterion (instead of
printf ), then both
make testsand GitHub will run them
automatically (GitHub only when you git pushyourwork).
emember to regularly
R git commityour work. Occasionally, git pushyour work to run
the same tests on the git serverand to submit yourwork for gradingby the due date.
Grading Notes
uring grading, only your
D ceasar.cfile will be copiedinto the grading framework’s
directory for processing. Make sure all of your code is self-contained in that file.
cademic honesty is taken very seriously in this course. By submitting your work for
A
grading you indicate your understanding of, and agreement with, the following
Academic Honesty Statement: