For this lab, write the following string manipulation functions. Do not use the functions in the string.h library for this assignment. (All of these functions have equivalents in the standard string library.) Test the functions in any way that you see fit, but test them thoroughly. Do not turn in untested functions! Be sure to test unusual cases, such as empty strings. You can write an empty string in your program as "".
Turn in your functions and your test program. The test program should test all of the functions. Don't throw away tests after they work.
Some functions were written in the lecture. You may feel free to use the lecture versions of those functions.
strcpy(s, "abcd"); strcat(s, "xyz");should end with array s holding null-terminated string "abcdxyz". Function strcat should presume that enough room is available in array s.
-1 | if s comes before t in alphabetical order |
0 | if s and t are identical |
1 | if s comes after t in alphabetical order |
Think about how to compare strings. You start with the first character of each string. You only continue to the next character if those characters are the same.
If s is a prefix of t (for example, if s is "abc" and t is "abcde"), then strcmp(s,t) should return -1. Similarly, if t is a prefix of s, then strcmp(s,t) should return 1.
You should use the standard ascii ordering of characters. You can compare two characters using comparisons such as < and >. Note that this will cause strcmp("DD","aa") to return -1, since character 'D' has a smaller ascii code than character 'a'. That is ok.
Note: There is a standard library function called strchr that behaves similarly to this function, but not quite the same. It returns the memory address of the character rather than its index, and returns memory address 0 if the character does not occur in the string.
Note: Function strchr should work correctly when character c is the null character. It should return the index where the null character is located (at the end of the string). Be careful about this, since it is easy to get wrong.
You can easily use tests that do not require user interaction. Just test your functions on some fixed strings. For example,
cout << "Begin strchr tests.\n"; i = strchr("abcd", 'c'); cout << "Test 1. " << i << " should be 2\n"; ...performs one test of strchr. It prints the answer, requiring the user to check whether the answer is correct. You can even make the tester check its own answers, and be silent if all goes well. That way, to run the test you don't need to wade through a lot of uninteresting output. You see only the cases where something goes wrong. Write something like
cout << "Begin strchr tests.\n"; i = strchr("abcd", 'c'); if(i != 2) cout << "Test 1. " << i << " should be 2\n"; ...
Software designers develop a large body of tests like this for their software, and run all of the tests frequently to be sure that new changes have not ruined something that used to work. For example, Microsoft runs a full test of everything every night, letting the software developers inspect the results when they come back to work in the morning. Such test suites are called regression tests.