Lab 5: String functions Intro

Overview

Previously in studying C++ you may recall the string class, with helpful built-in features. We don't have all that in C, but we do have lots of functions that operate on NULL-terminated arrays of char, which is how we implement strings in C.

Reading

As is true for each week, whether or not we remind you about it in the lab write-up, read the items listed below to prepare for this week's lab. The reading will be the basis for a short quiz at the beginning of your lab. As you read, remember to write down the main points in an outline (about a written half-page total, or 260+ words), which you will use with a partner in lab to help you create a tutorial. You will turn in this outline by the end of Monday, at midnight, using the Reading Outline form.

The readings for this week are from Programming in C, 4th Edition, by Stephen Kochan, available for free through UIC's O'Reilly Safari library. For your convenience the excerpted pages listed below for this week are available in Piazza in the Resources tab here.

24 pages total (though they're not very dense, and the 5 pages about pointers are review):

    • 9. Character Strings (pp. 235-249) 15 pages

    • 10. Review Pointers (pp. 279-283) 5 pages

    • 10. Pointers to Character Strings (pp. 313-314) 2 pages

    • Appendix B. String Functions (pp. 537-539) 2 pages

If you want to access the entire book, when you access the O'Reilly Safari login page you will get a prompt to select your institution. UIC is not listed, so instead select the link for "Institution not Listed?" and provide your UIC email. On the O'Reilly books page search for the desired title.

Lab Instructions / Questions


In your lab-partner slides (besides your partner information, title, importance, and references), in your examples section provide the following:


  1. Take a look at the various functions in the C string.h library. Which 6 of them would be most useful for the program described below? Sort them in order of usefulness, with the most useful listed first, then the next most useful, and so on. For each one give a brief code example for how it might be useful in this program.

    Program where the string functions would be used:
    Imagine you are writing a word-guessing program (sort of like
    Wordle with varying length words), where the program selects some dictionary word that the user must guess. The user enters a series of valid dictionary word guesses of any length, and for each guess the program indicates which letters are somewhere in the secret word, and additionally which letters in the current guess are in their correct position.

  2. Write a program that, given a string of input words, prints out how many words there are, followed by the individual words, one per line. Running the program might look like:


Enter an input string: Life's second chance is tomorrow.

These 5 words found:

Life's

second

chance

is

tomorrow.


Running the program again might look like:

Enter an input string: All generalizations are false.

These 4 words found:

All

generalizations

are

false.


Include a link to your program in Replit, using the Replit Lab 5 starter code program. On your lab partner slides don't post your whole program, but rather describe the key features of your program that make it work.

Starter Code

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