CSE 220: Systems Fundamentals I
tony Brook University
S
Homework Assignment #3
FALL 2023
Due: Wednesday Oct 11 at 11: 59 PM EST
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.
Implement the following string functions in file strgPtr.c.
int strgLen( char* s ): return the length of strings.
●
● void strgCopy( char* s, char* d ): copy the contentof string s (source) to
d (destination).
● v oid strgChangeCase( char* s ): for each character in the string, 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.
● int strgDiff( char* s1, char* s2 ): compare stringss1 and s2. Return the
index where the first difference occurs. Return -1 if the two strings are
equal.
● void strgInterleave( char* s1, char* s2, char* d ):copy s1 and s2 to d,
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”
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).
Content of s ontent of d (after calling the
C
function)
“ Computer Science” “Computer Science”
“CSE-220” “CSE-220”
“System Fundamental” “System Fundamental”
“1” “1”
“”/empty “”/empty
Note:
1- In case any of s or d is NULL, do nothing and return from the function
2- You should copy everything including the null terminator
3- If d does not have enough space to hold s, copy until you reach the last index of d
and make it null-terminated. For example if d is enough for 5 bytes and you have
“Computer Science” in s, after calling the function, d should have “Comp”
● 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”
Content of s1 Content of s2 ontent of d (after
C
calling the function)
“ abc” “ 123” “a1b2c3”
“abcdef” “123” “a1b2c3def”
“cse” “12345” “c1s2e345”
“1234” “cs” “1c2s34”
“”/empty “”/empty “”/empty
“” “123” “123”
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.
Encryption and Decryption
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:
plaintext: “System Fundamentals”
ciphertext before function call: “I can store any message!”;
ciphertext after function call with Key=1: “Tztufn Gvoebnfoubmt__EOM__”
Return value : 18
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)
Ciphertext Test Cases
Plaintext Key iphertext After
C Return Value
Function Call
bc
a
2 cde__EOM__
3
Ayb 3 Dbe__EOM__ 3
Cse220 1 Dtf331__EOM__ 6
CS 0 CS__EOM__ 2
empty Any key undefined__EOM__ 0
Part B: Decryption
int decrypt(const char *ciphertext, char *plaintext, int key)
Ciphertext Test Cases
Ciphertext Key laintext After
P Return Value
Function Call
c de__EOM__
2 abc
3
Dbe__EOM__ 3 Ayb 3
Dtf331__EOM__ 1 Cse220 6
CS__EOM__ 0 CS 2
empty__EOM__ Any key undefined 0
Running, Testing and Submitting Your Code
unning test cases using Criterion is optional for this homework. You can have your way of testing the
R
code. However, as we move to the next assignment we will adopt this framework for testing the
homework. The testing framework is provided in the starter code. Try it and familiarize yourself.
To run the provided unit tests (written usingCriterion):
●
maketo build your code
○ThedirectorystructureforthishomeworkassignmentisdifferentfromHW#2.
Binaries are now located in the
bin directory. If you want to understand
ore, feel free to explore the provided
m .
makefile
●make testto test your code
●
make gcovto check the test coverage (see below)
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.
xtensions, resubmissions and regrades will not be granted because a student
E
did not use git properly to submit the assignment.
Academic Honesty Policy
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:
. I understand that representing another person’s work as my own is academically
1
dishonest.
2. I understand that copying, even with modifications, a solution from another source
(such as the web or another person) as a part of my answer constitutes plagiarism.
3. I understand that sharing parts of my homework solutions (text write-up, schematics,
code, electronic or hard-copy) is academic dishonesty and helps others plagiarize my
work.
4. I understand that protecting my work from possible plagiarism is my responsibility. I
understand the importance of saving my work such that it is visible only to me.
5. I understand that passing information that is relevant to a homework/exam to others
in the course (either lecture or even in the future!) for their private use constitutes
academic dishonesty. I will only discuss material that I am willing to openly post on the
discussion board.
6. I understand that academic dishonesty is treated very seriously in this course. I
nderstand that the instructor will report any incident of academic dishonesty to the
u
University’s Academic Judiciary.
7. I understand that the penalty for academic dishonesty might not be immediately
administered. For instance, cheating on a homework assignment may be discovered
and penalized after the grades for that homework have been recorded.
8. I understand that buying or paying another entity for any code, partial or in its entirety,
and submitting it as my own work is considered academic dishonesty.
9. I understand that there are no extenuating circumstances for academic dishonesty. 8