The purpose of this assignment is to help you learn/review (1)
arrays and pointers in the C programming language, (2) how to create
and use stateless modules in C, (3) the "design by contract"
style of programming, and (4) how to use the GNU/UNIX programming
tools, especially bash
, emacs
, gcc,
and gdb
.
Implement the string functions listed by the Table in Part 1. Part 2 is the "on your own" part of this assignment, which is worth 50% of this assignment.
You will get an extra 10% of the full score if you implement the Part 1 only with pointer notation when you access the character. See the Extra credit section below (Extra Credit for Part 1).
As you know, the C programming environment provides a standard library. The facilities provided in the standard library are declared in header files. One of those header files is string.h; it contains the declarations of "string functions," that is, functions that perform operations on character strings. Appendix D of the King textbook, Appendix B3 of the Kernighan and Ritchie textbook, and the UNIX "man" pages describe the string functions. The string functions are used heavily in programming systems; certainly any editor, compiler, assembler, or operating system created with the C programming language would use them.
Your task in this assignment is to use C to create the "Str" module that provides string manipulation functions. Specifically, design your Str module so that each function behaves the same as described below. Your task in this assignment is twofold.
[Part 1] Read the description of the basic string library functions carefully, and implement each function. The basic functions are most commonly used standard string functions. Each function should behave the same as its corresponding standard C function.
[Part 2]
Implement a simplified version of grep
using Str
functions. Read the provided file that contains
skeleton code carefully, edit the file to make it process the required
functionalities: find, replace, diff.
Str
Function Implementation Your task for the first part is to use C to implement five basic
string manipulation functions: StrGetLength(), StrCopy(),
StrCompare(), StrSearch(), StrConcat()
. Those five functions
should follow the format of the corresponding standard C library
functions. You can easily find the function's description and
operation in the UNIX "man" page.
The following table shows the required basic string functions in Part 1 and their corresponding function names in the standard C library.
Str Function
|
Standard C Function | Man page link |
---|---|---|
StrGetLength
|
strlen
|
strlen man page |
StrCopy
|
strcpy
|
strcpy man page |
StrCompare
|
strcmp
|
strcmp man page |
StrSearch
|
strstr
| strstr man page |
StrConcat
|
strcat
|
strcat man page |
Use the Str
module's interface in a file named str.h
, and place your Str
function definitions
in str.c
.
Note that your Str functions should not call any of the standard string functions. In the context of this assignment, pretend that the standard string functions do not exist. However, your functions may call each other, and you may define additional (non-interface) functions.
Design each function definition so it calls the assert
macro to validate the function's parameters. In that way,
your Str
functions should differ from the standard C
string functions. Specifically, design each function definition to
assert that each parameter is not NULL
. See the
note below for more information of
the assert()
macro function.
Beware of type mismatches. In particular, beware of the difference between type size_t and type int: a variable of type size_t can store larger integers than a variable of type int can. Your functions should (in principle) be able to handle strings whose lengths exceed the capacity of type int. Also beware of type mismatches related to the use of the const keyword.
There are various ways to implement the functions in Part
1. Especially, you can access the character by pointer dereferencing
like *pcSrc
or by using an array notation such
as pcSrc[uiLength]
.
Here are two examples of StrGetLength()
implementation. The first code implements the StrGetLength() function
with the array notation; it traverses each given string or accesses
the character using an index relative to the beginning of the string.
However, with the pointer notation, the second version traverses each
given string using an incremented pointer.
You can freely implement Part 1. However, if you
implement part 1 only with pointer notation, you can get extra 10% of
the full score. Note: if you simply convert array
notation to pointer notation (e.g., *(p+i) instead of p[i]), that does
not count as pointer notation. That is, pointer notation should
not deal with an index. We will strictly apply the criteria for
pointer notation, so please avoid using notation like (p+i) to get the
full credit. p++/-- and *p are just fine. Please write your choice of
implementation in the readme
file. That is, specify if
you used only the pointer notation for Part 1 in
the readme
file.
We provide a test client
(client.c
) that compares
the results of the Str
and C standard library functions
with various input. Please use this code for testing and
debugging. You can compile the test client with gcc209
as
follows.
Str
functions separately by providing
a function name as an argument. For example,
will test StrCopy
. Actually, the client accepts any one of the Str
function names as a command-line argument:
Note that passing all tests provided by the client does not mean that your function always behaves correctly. Please devise your own testing (e.g., by changing the client code) for more confidence. Note that we may use a different test client for grading.
grep
is a popular UNIX tool that manipulates input strings. In this part, you implement a simplified version of grep
called sgrep
. sgrep
provides these three functionalities.
-f search-string
): reads each line from standard input (stdin
) and prints out only the lines that contain search-string
to standard output (stdout
). -r string1 string2
): reads each line from standard input (stdin
), replaces all occurrences of string1
with string2
, and prints it out to standard output (stdout
). If string1
is not found in the line, the original line is copied to stdout
. -d file1 file2
): compares the two files (file1 and file2) line by line, and prints out only the different lines with their line numbers to standard output (stdout
). The output format of the different line should be as follows:
Here are some usage examples of sgrep
(you can also see the test files (google_wiki.txt
and microsoft.txt
) which are used in the following example):
Rules:
search-string, string1, string2
) refers to a sequence of any non-space characters. It can be enclosed with double quote(") characters and can be either empty ("") or can include a space (e.g., "hello world"). No need to handle an escape character for this assignment. stderr
). Use fprintf(stderr, ...);
for that.sgrep.c
). string1
cannot be an empty string. In such a case, your program should stop with a proper error message (e.g., "Error: Can't replace an empty substring"). string2, search-string
) can be an empty string. That is, an empty search string matches any line, and if string2
is an empty string, it removes any occurrences of string1
in the matching line.
Tips:
sgrep.c
). You can start with the file. fgets
should be useful for reading a line from a file. 'man fgets' should give you more information. Develop on lab machines using emacs
to create source code and gdb
to debug.
For part 1, you implement the code str.c
and str.h
, which should contain the definitions of required functions.
For part 2, a skeleton C file is available here: sgrep.c. It implements the basic I/O and argument parsing, so all you need is fill out the rest of the functionalities with Str
functions. Feel free to use this file as your starting point (of course, you don't have to use it), and change any part in the code if you'd like.
Create a readme
text file that contains:
StrCopy
to call assert
to verify that the destination memory area specified by the caller is large enough? Explain.readme
text file whether you use pointer notation for the extra credit.
Use KAIST
KLMS to submit your assignments. Your submission should
be one gzipped tar file whose name is
YourStudentID_assign2.tar.gz
For example, if your student ID is 20191234,
please name the file as
20191234_assign2.tar.gz
Create a local directory named 'YourStudentID_assign2
' and place all your files in it. Then, tar
your submission file.
Please refer here for how to archive your assignment.
Your submission need to include the following files:
str.c
and str.h
filessgrep.c
filereadme
text fileYour submission file should look like this:
We will grade your work on quality from the user's point of view and from the programmer's point of view. To encourage good coding practices, we will deduct points if gcc209
generates warning messages.
From the user's point of view, your module has quality if it behaves as it should.
In part, style is defined by the rules given in The Practice of Programming (Kernighan and Pike), as summarized by the Rules of Programming Style document. These additional rules apply:
Names: You should use a clear and consistent style for variable and function names. One example of such a style is to prefix each variable name with characters that indicate its type. For example, the prefix c
might indicate that the variable is of type char
, i
might indicate int
, pc
might mean char*
, ui
might mean unsigned int
, etc. But it is fine to use another style -- a style which does not include the type of a variable in its name -- as long as the result is a readable program.
Line lengths: Limit line lengths in your source code to 72 characters. Doing so allows us to print your work in two columns, thus saving paper.
Comments: Each source code file should begin with a comment that includes your name, the number of the assignment, and the name of the file.
Comments: Each function should begin with a comment that describes what the function does from the caller's point of view. The function comment should:
Parameter Validation: Validate function parameters via asserts whenever possible.
assert is a macro implementations of assertion, used for verifying the conditions. If the condition is true, it does nothing. However, if the conditions is FALSE, it displays an error messages and aborts the running program.
In this assignment, you should validate the function's parameters with assert
. When you try to check whether your Str
functions validate the given parameters correctly or not, the aborted program may annoy you. In that case, you can add NDEBUG
macro in your source file to ignore the assert functions. Otherwise, you can also add -D NDEBUG argument to the gcc
. The -D NDEBUG
argument commands gcc209
to define the NDEBUG
macro, just as if the preprocessor directive #define NDEBUG
appeared in the specified .c file(s). Following OBcommands are example of disabling assert using -D option
If NDEBUG is defined as a macro name in the source file, the assert macro is defined as ((void)0)
, which means that the assert macro will be ignore.
C programmers sometimes use idioms that rely on the fact that the null character ('\0'), the NULL
address, the integer 0, and the logical concept FALSE have the same representation. You may use those idioms. For example, you may define StrGetLength
like this:
or like this:
But you are not required to use those idioms. In fact, we recommend that you avoid the use of idioms that adversely affect understandability.
const
Keyword and StrSearch
The use of the const
keyword within StrSearch
is tricky, as this question-and-answer sequence indicates.