Systems Programming (XMUT)
Assignment 1: C programming
Resources and links
Do not rename these files.
This assignment is divided into 2 parts.
- In Part I, you will be asked to answer questions about Weeks 1--3 topics, submitted in a plain text file named
part1.txt
- In Part II, you will be asked to implement C functions, submitted in a file named
editor.c
Part 1: Concepts (30%)
This part will test your conceptual knowledge of C fundamentals, including operators, arrays, and strings. Your answers must be submitted in a plain text file named
part1.txt
.
Core [20 Marks]
- [4 Marks] Are the following valid or invalid C identifiers?
-
while
-
record_100
-
$record
-
integer-counter
- [8 Marks] Suppose
a
, b
and c
are integer variables that have been assigned the values a = 7
, b = 3
and c = 5
. What is the value of each of the following expressions?
-
a + b + c
-
a / b
-
a % b
-
a * b % c
- [8 Marks] Suppose
cl
, c2
and c3
are character-type variables that have been assigned the characters 'D'
, '4'
and '?'
, respectively. Based on the ASCII character set, what is the numerical value of each of the following expressions?
-
c1 + c2 + c3
-
c1 - 'A'
-
3 * c2
-
'3' * c2
Completion [7 Marks]
- [1 Marks] Given the following C code snippet, what is the type of the expression
c+i+l
? Briefly explain your answer.
char c;
int i;
long l;
- [2 Marks] Given the following C code snippet, what is the value assigned to
k
in the 2nd line? Briefly explain your answer.
int i = 8, j = 6, k;
k = (j > 5) ? i : j;
- [2 Marks] What is the problem (if any) in the following C statement?
int rem = 10.0 / 4 % 2;
- [2 Marks] Rewrite the following code, using a for-loop, to produce the same output.
int main (void)
{
int j = 5;
while(j >= 0)
printf("%d ", --j);
return 0;
}
Challenge [3 Marks]
- [1 Marks] What is the value of
i
, j
, and k
after the last statement in the following C code snippet? Explain your answer by showing step-by-step solution.
int i = 5, j = 10, k = 1;
(k += 3*--i) - j++;
- [2 Marks] What is the output of the following code fragment? Explain your answer.
char string[] = "One\0Two\0Three";
printf("%d", strlen(string));
Part 2: Practical Programming (70%)
This part will test your application of the conceptual knowledge of C fundamentals to solve practical programming tasks. You may only use the Standard C Library to perform the tasks in this part. You must implement the functions in a file named
editor.c
.
The programming tasks involve the implementation of several basic
text editor operations:
insert,
delete,
replace, etc. An important component of a text editor is the
editing buffer which can be viewed as a one-dimensional array of characters.
The functions you will be implementing deal with manipulating the contents of the editing buffer:
- for Core (Tasks 1 and 2), you will implement
editor_insert_char
and editor_delete_char
;
- for Completion (Task 3), you will implement
editor_replace_str
.
Sample code showing an example of how you can test your code is provided under the files
directory in the archive that contains this file.
You should provide appropriate comments to make your source code readable. If your code does not work and there are no comments, you may lose all marks.
Coding Style
You should follow a consistent coding style when writing your source code. Coding style (aka coding standard) refers to the use of appropriate indentation, proper placement of braces, proper formatting of control constructs, and many others. Following a particular coding style consistently will make your source code more readable.
There are many coding standards available (search "C coding style"), but we suggest you consult the
lightweight Linux kernel coding style (see
coding-style). The relevant sections are Sections 1, 2, 3, 4, 6, and 8. Note that you do not have to follow every recommendation you can find in a coding style document, you just have to apply that style consistently.
Task 1: Core [25 Marks]
Implement a function with the prototype
int editor_insert_char(char editing_buffer[], int editing_buflen,
char to_insert, int pos);
which will insert the character
to_insert
at index
pos
of
editing_buffer
. The size of
editing_buffer
is
editing_buflen
. When a character is inserted at index
pos
, each of the original characters at index
pos
until the end of the buffer must be moved by one position to the right. The last character is thrown out. The function should return 1 if the character insertion occurred, otherwise it should return 0.
For example, if
editing_buflen
is 16 and the contents of
editing_buffer
are
after executing
int r = editor_insert_char(editing_buffer, 16, 's', 12);
the value of
r
should be 1 and contents of
editing_buffer
should be
You can test your implementation by compiling
editor.c
together with
t1test.c
(provided under the files directory). If
everything goes well, this will generate an executable file t1test. To see if your implementation is correct, run
t1test
and compare the expected and actual buffer contents and return values. If they match, it means your implementation passes the test. You are free to modify
t1test.c
if you want to add in more test cases.
Task 2: Core [25 Marks]
Implement a function with the prototype
int editor_delete_char(char editing_buffer[], int editing_buflen,
char to_delete, int offset);
which will delete the first occurrence of the character
to_delete
. The search should start from index
offset
of
editing_buffer
. The size of
editing_buffer
is
editing_buflen
. When a character is deleted at index
pos
, each of the original characters at index
pos
until the end of the buffer must be moved by one position to the left. A null character (
'\0'
) is inserted at the end of the buffer. The function should return 1 if the character deletion occurred, otherwise, it should return 0.
For example, if
editing_buflen
is 16 and the contents of
editing_buffer
are
after executing
int r = editor_delete_char(editing_buffer, 16, 'o', 6);
the value of
r
should be 1 and the contents of
editing_buffer
should be
You can test your implementation by compiling
editor.c
together with
t2test.c
(provided under the files directory). Make sure that
editor.c
and
t2test.c
are in the same directory. If everything goes well, this will generate an executable file
t2test
. To see if your implementation is correct, run t2test and compare the expected and actual buffer contents and return values. If they match, it means your implementation passes the test. You are free to modify
t2test.c
if you want to add in more test cases.
Task 3: Completion [20 Marks]
Implement a function with the prototype
int editor_replace_str(char editing_buffer[], int editing_buflen, const char *str, const char *replacement, int offset);
which will replace the first occurrence of the string
str
with
replacement
.
The search for the first occurrence should start from index
offset
of
editing_buffer
. The size of
editing_buffer
is
editing_buflen
.
The replacement should not overwrite other contents in the buffer. This means that if
replacement
is longer than
str
, there is a need to move the characters after
str
to the right. Likewise, if
replacement
is shorter than
str
, there is a need to move the characters after
str
to the left. When moving characters to the right, throw out characters that will not fit in the buffer and when moving characters to the left, insert null characters in the vacated positions.
If
str
is empty (regardless of the value of
replacement
), no string replacement should occur. If
replacement
is empty, then this is the same as deleting the string
str
.
If the replacement text will go beyond the limits of
editing_buffer
, then replacement should only occur until the end of
editing_buffer
.
Ensure that the last character in the buffer is always the null character.
If the string replacement occurred, the function should return the index corresponding to the last letter of
replacement
in
editing_buffer
, otherwise, it should return -1. If the replacement text will go beyond the limits of
editing_buffer
, the function should return
editing_buflen-1
.
For example, if
editing_buflen
is 16 and the contents of
editing_buffer
are
After executing
int r = editor_replace_str(editing_buffer, 16, "World!", "there", 0);
the value of
r
should be 11 (which is the index of the last 'e' in "there") and the contents of
editing_buffer
should be
You can test your implementation by compiling
editor.c
together with
t3test.c
(provided under the files directory). Make sure that
editor.c
and
t3test.c
are in the same directory. If everything goes well, this will generate an executable file t3test. To see if your implementation is correct, run
t3test
and compare the expected and actual buffer contents and return values. If they match, it means your implementation passes the test. You are free to modify
t3test.c
if you want to add in more test cases.
Marking Criteria for Part 2:
Criteria |
Weight |
Expectations for Full Marks |
Compilation |
5% |
Compiles without warnings |
Comments |
10% |
Sufficient and appropriate comments |
Code Quality |
15% |
Consistent coding style |
Correctness |
70% |
Handles all test cases correctly |
|
100% |
|
For the
Correctness criteria, the following table shows the marks distribution over the different task types:
Task Type |
Marks |
Core (task 1) |
25 |
Core (task 2) |
25 |
Completion (task 3) |
20 |
Total |
70 |