C-String Assignment Homework 1 Defining C-string Functions Due Date: Tuesday, January 19 at 11:00 pm. Point Value: 20 points In this homework assignment, you must define a number of functions that can be used to analyze and manipulate C-strings. Background We think of a string as a sequence of characters. The term C-string refers to the classic implementation of strings in the C programming language. A C-string is a sequence of characters terminated by the null character, '\0'. Since C is part of C++, C-string implementations are valid in C++ as well as C. Recall that an array of characters is the underlying data structure for storing C-strings. For example, this definition creates such an array. char myString[100]; myString will be capable of holding a C-string with up to 99 characters before the terminating null character. Of course, myString can hold a shorter C-string, too. For example, these assignments give myString the value "xyz". myString[0] = 'x';
myString[1] = 'y';
myString[2] = 'z';
myString[3] = '\0'; It is legal to initialize a string variable, like this. char example[100] = "First value"; However, it is not legal to assign string variables, because you cannot assign to an entire array. myString = "xyz"; // ILLEGAL: Cannot assign to an array Furthermore, you cannot do comparisons like this. if (myString == "xyz")
cout << "fantasy land" << endl; The comparison myString == "xyz" is actually legal (it will compile), but it will always evaluate to false. To handle these kinds of difficulties, programmers can rely on the C-string library. This library, which is part of every proper C++ installation, is an extensive collection of functions for handling C-strings. The UNIX man pages list all of the function prototypes in this library along with some documentation on their use. eos% man string However, for this particular assignment you are prohibited from using the C-string library itself. (Do not use #include in your code.) Your assignment The functions that you must define are listed below. All of the character manipulation done by your functions must be done using the arrays passed into the functions as arguments. You may not define any additional arrays in your functions or globally. For all of these descriptions, the arguments s, s1, s2 are C-strings. The functions, reverse() and insert(), alter their first argument. The insert function does not check for overflow of the array of the first argument. Prototype Description int length(const char s[]); Returns the length of s, which is the number of characters in s, not including the terminating null character. The null string would have a length of 0. void reverse(char s[]); Reverses the order of the characters in the string s. void insert(char s1[],
const char s2[], int n); Inserts the string s2 in the string s1 immediately following the nth character in s1. If n is 0, s2 is inserted before the first character in s1. If n is greater than the number of characters in s1, s2 is inserted after the last character in s1. bool search(const char s1[],
const char s2[]); Searches for the string s2 in the string s1. If s2 is a substring of s1, true is returned (i.e., return true;). Otherwise, false is returned (i.e., return false;). The null string is always a substring of any other string. Here are some examples of how each function can be used. Assume the string variables a, b, c, d, and e are defined like this. const int MAX_STR_LEN = 99;
char a[MAX_STR_LEN + 1] = "Hello"; //Note: These = signs represent
char b[MAX_STR_LEN + 1] = "Goodbye"; //initializations, not assignments,
char c[MAX_STR_LEN + 1] = "1234"; //and are therefore legal.
char d[MAX_STR_LEN + 1] = "1236789";
char e[MAX_STR_LEN + 1] = "CSC";
Function Call Result or Return Value length("abc") 3 length("") 0 length(a) 5 reverse(a); a becomes "olleH". a[5] remains '\0'. insert(b, " Bob", 20); b becomes "Goodbye Bob". b[11] = '\0'. insert(c, "0", 0) c becomes "01234". c[5] = '\0'. insert(d, "45", 3); d becomes "123456789". d[9] = '\0'. insert(e, "210", 3); e becomes "CSC210". e[6] = '\0'. search(d, "123") true search("abcabcdabcde", "bcd") true search("abcabcdabcde", "xy") false search("abcde", ""); true Putting the assignment together Create a single source code file named strings.cpp. It should contain the definitions of these functions. It may contain the function prototypes as well as prototypes and definitions of other "helper" functions, but it may not contain a main(). We will provide a driver (the file /afs/eos.ncsu.edu/info/csc/csc210_info/www/Homework/1_HW/driver.cpp) that you can use for some minimal testing of your code. You can modify the driver to do more extensive testing of your functions. To compile your functions and the driver into an executable, use this UNIX command. eos% g++ -o driver driver.cpp strings.cpp This will create an executable program named driver that you can run as follows. eos% driver Submitting your work Refer to the course submit page for details on using the departmental submit command. The name of this assignment is 1_HW. Submit the file strings.cpp only. Do not submit an entire directory. Do not submit additional files. Your code must be submitted on or before 11:00 pm. on Tuesday, January 19. Recall also that starting with this homework you are required to turn in a paper copy of your program(s) at the beginning of the first lecture after the due date. A good way to print programs is with the a2ps utility (keywords are printed in boldface and comments in italics).
% add psutils
% a2ps -d strings.cpp
will do the trick (the -d option says to send the output to the printer; there are many other options: see our list of some of the more popular ones). Be sure to fill in and staple a copy of the Homework 1 Grading Sheet to the front of your listing. Grading Your code should pass all of the tests given in driver.cpp. However, passing those tests is not sufficient to guarantee 100 in the assignment. We will create a separate driver file for testing your code for grading. You may assume the grading driver will test the functions according to their documentation in this page. (There will be no attempt in the grading driver to generate string overflow.) The total point value for this assignment is 20 points. Points will be given for correctness of code as follows. Function Point value length 5 reverse 5 insert 5 search 5 In addition to the correctness points, your grade depends on these restrictions. Your code should conform to CSC 210 style guidelines. Major deviations in these guidelines (including poor choices of variable names or indention that does not reflect the control structures in the code) may result in deductions from your score. Your code must compile. You will not receive any credit for code containing syntax errors. Do not expect the graders to look for or to correct your errors. It is entirely your responsibility to submit legal code. For this assignment only, you will not receive any credit for code that uses the built-in C-string library.