CSCI 240 - Assignment 10 -
Spring 2007Due: Wednesday, April 18, 3 pm
100 points
Overview
For this assignment you are to write character-array-based string functions to replace several of the standard C++ string library functions and a couple of other utility functions as well. Your main() function will test these these functions by calling them and displaying the results.
Note: you should write these functions one at a time. As you write them, add code to main() to call and test them. Only when the newly-added function works should you move on to do the next.
DO NOT use any subscript notation in these functions. Use pointer notation throughout. The only place that [] should appear is in array declarations.
The Functions
int my_strlen(char *source)
This function returns the number of characters in the string pointed to by source. The null terminator is not included in the character count.
void my_strlower(char *source)
This function will convert any uppercase letters in source to lowercase. It will not change any other characters. You may use the standard C++ character function tolower() in this function.
void my_strupper(char *source)
This function will convert any lowercase letters in source to uppercase. It will not change any other characters. You may use the standard C++ character function toupper().
void my_strcpy(char *destination, char *source)
This function will copy the string pointed to by source (including the null terminator) into the string pointed to by destination. You may assume that destination has room for source. That is, you don't have to check to be sure that there is room.
char * my_strchr(char *source, char search_character)
This function searches the string pointed to by source for the first occurrence of search_character. If search_character is found, the function will return a pointer to the first occurrence of search_character in source. If search_character is not found, the function will return the null pointer:
return NULL;
The calling program can then test the returned pointer:
if (p == NULL) ...
to determine if an occurrence of search_character was found.
int my_replaceFirst(char *string, char target, char replacement)
This function should replace the first occurrence of target in string with replacement. For example, if string is "April 18, 2007" and target is '8' and replacement is '9' then after the function call, string should contain "April 19, 2007". The function should return 1 if it can find target in string, and 0 if it cannot find target in string. You should use my_strchr() in this function.
void my_replaceAll(char *string, char target, char replacement)
This function should replace all occurrences of target in
string with replacement. For example, if string is "***
Function Menu ***" and target is '*' and replacement is "-" then
after the function call, string should contain "---Function Menu ---".
The function returns nothing: if string does not contain target, nothing is
done. This function should call my_replaceFirst() in a loop to
accomplish its work.
Your main() Function
Declare two 80-character arrays, s1 and s2, to be used by the functions you write. Initialize s1 to "Fourscore And Seven" and s2 to "The Bulls won 4 games in a row!"
Test your functions as follows, in the order given, with the data specified.
Each step should display a step number and a clear description of what is happening. If the program does not clearly explain what it is doing at each step, you will lose credit.
Whenever possible, use variable values, not hard coded values. For example, Step 1 below should display:
1. The length of "Fourscore And Seven" is 19
and the code to produce it could be:
cout << "\n1. The length of \"" << s1 << "\" is " << my_strlen(s1);
Notice the use of "/"" to display a single " inside a string literal. That's double-quote backslash double-quote double-quote. Use it for all displays of s1 and s2.
1. Call my_strlen() on s1 and display the length.
2. Display the value of s1 and then call my_strlower() on s1 and display the new value.
3. Display the value of s1 and then call my_strupper() on s1 and display the new value.
4. Do the same (i.e. step 2 and 3) for s2.
5. Use my_strcpy() to copy the literal string "xyzzy is the magic word!" to s1 and then display s1. (Here the label may include the literal string.)
6. Use my_strchr() to look for a 'z' in s1. If it is found, main() should report "found" otherwise report "not found".
7. Use my_strchr() to look for a 'q' in s1. If it is found, main() should report "found" otherwise report "not found".
8. Use my_replaceFirst() to replace the first 'm' with 'M' in s1. Also replace the '!' with a '?'. Display the result. If the m or ! is not found, display a brief error message.
9. Use my_replaceAll() 2 times to replace all the 'y' chars with '#' and all the 'z' chars with '&' in s1. Display the result.
Notes:
1. You may not use any standard C++ string or character functions in this program except toupper() and tolower().
2. Document your program and turn in the usual.