Lab 6: Writing String Functions

Overview

Knowing how to use C string functions is helpful. Writing our own versions of C string functions can give us a deeper understanding of using arrays and pointers. This week in lab the focus is on writing code.

Reading

Turn in reading outlines by the end of Monday, at midnight, using the Reading Outline form.

This reading will help us understand and use the C-string library definitions. (In the reading you can ignore the explanations that are Microsoft-specific or are C++ specific.) Your outline for this week should highlight how the different implementations shown in the reading work, along with an outline of specific ideas that might work for code you need to implement in lab in the functions removeString(), insertString(), and any utility functions you write. We highly encourage you to actually write code ahead of time for at least the utility functions, as it can be easy to get bogged down during lab when you have limited time.

This week's reading material was created by Delroy A. Brinkerhoff from Weber State University, from Chapter 8 of his online text Object-Oriented Programming using C++.

Lab Instructions / Questions


This week the focus is on writing code, both in the preparation as well as during the lab activity.


In your lab-partner slides (besides your partner information, title, importance, and references), be sure to include a link to your program in Replit, where you used the Replit Lab 6 starter code and run the two test cases. On your lab partner slides don't post your whole program, but rather describe the key features, explanations, and lessons learned along the way to making your code work.

In your examples section start by explaining how far you got in your implementation. Document the problems you encountered as you created your own code, and how you resolved them.


The focus of your work is to implement functions removeString()and insertString(). These are documented below. You will likely benefit from also implementing utility functions for length, myCopy, mynCopy, and append.

  1. Write a function called removeString() to remove a specified number of characters from a character string. The function should take two arguments: the source string and a string to remove. So the code
    char text2[] = "All generalizations are false";
    removeString (text2, "generaliz");
    has the effect of removing the characters
    "generaliz" from the array text. The resulting string inside text is then
    "All ations are false".

  2. Write a function called insertString() to insert one character string into another string. (Assume enough space has been allocated in the destination string to hold the insertion.) The arguments to the function should consist of the source string, the string to be inserted, and a pointer to the position in the source string where the string is to be inserted, shifting contents after it to the right. So, the code
    char text3[] = "All ations are false";
    char *pFound = findString (text3, "ations");
    insertString (text3, "specific", pFound);
    which results in
    text3 containing the string "All specifications are false"

Documentation on other functions (which you don't have to write)


Functions findString() and replaceString() are provided for you. For findString() study it to to see how it determines if one character string exists inside another string. The first argument to the function should be the character string that is to be searched and the second argument is the string you are interested in finding. If the function finds the specified string, have it return a pointer to the location in the source string where the string was found. If the function does not find the string, have it return NULL. So, for example, the call
char text1[] = "All generalizations are false";
char *pFound = findString (text1, "general");
searches the string
"All generalizations are false" for the string "general". Because "general" does exist inside the source string, the function returns a pointer to the character 'g' indicating the starting position inside the source string where "general" was found.


Once you have finished writing your functions, note how we use the findString(), removeString(), and insertString() functions to write a function called replaceString() that takes three character string arguments as follows:
replaceString (source, s1, s2); // In the source string replace the first s1 with s2
and that replaces the first string s1 found inside source with the string s2. The function should call the
findString() function to locate s1 inside source, then call the removeString() function to remove s1 from source, and finally call the insertString() function to insert s2 into source at the proper location. So, the code
char text4[] = "All generalizations are false";
replaceString (text4, "generaliz", "specific");
replaces the first occurrence of the character string
"generaliz" inside the character string text4, if it exists, with the string "specific", resulting in text4 having the string:
"All specifications are false"
Similarly, the function call
replaceString (text4, "*", "");
has the effect of removing the first asterisk inside the text array because the replacement string is the null string.

(The idea for this lab came from chapter 9 exercises in Programming in C, 4th Edition, by Stephen Kochan.)

Starter Code

As mentioned above, use the Lab 6 starter code in Replit, which looks like the following: