Topical Information

This lab will help you practice with C-strings and libraries. Function overloading might come in handy, as well.

Program Information

Two useful functions that are not provided in the standard library are find a character in a string and find a substring in a string.

For instance, the character 'e' appears at position 2 in the string "The quick brown fox". However, it does not appear in the string "cat's bat about yarn" at all. So our function could return a valid index if the character is found (2 above) or -1 (an invalid index) when it can't find the character.

Substrings are similar. The string "he" appears at position 1 in "The quick brown fox". But, again, it does not appear at all in "cat's bat about yarn". And we can return -1 or a valid index (such as 1 here) to indicate where the substring was found.

Write these two functions — both named find. Place them in a library called strextra (since the name string is already taken).

Some further examples:

    string              |  looking for       |  returned  |  which find
------------------------+--------------------+------------+--------------
 "The quick brown fox"  |  'e'               |       2    |  char
 "The quick brown fox"  |  "e"               |       2    |  C-string
 "The quick brown fox"  |  ' '               |       3    |  char
 "The quick brown fox"  |  "quick"           |       4    |  C-string
 "The quick brown fox"  |  "quiet"           |      -1    |  C-string

Other examples which can help to catch common coding errors:

    string              |  looking for       |  returned  |  which find
                        |                    |            |  is called
------------------------+--------------------+------------+--------------
 "The quick brown fox"  |  "cow"             |       7    |  string
 "The quick brown fox"  |  "hix"             |       1    |  string
 "11112"                |  "112"             |      -1    |  string

Next write a test application for this library. A test application is basically a driver that tests all the functions in the library.

Thought Provoking Questions

  1. What arguments does each find function take? Are they changed? What special care should you take with them?

  2. What value is returned by your functions? What type is it and what does it represent?

  3. What care does a caller of your functions have to take with this return value? (i.e. Can they immediately assume it is a valid index?)

  4. How does the compiler distinguish which of your functions is being used for a particular call? (They have the same name, after all...)

  5. How do you protect your library from being circularly included?

  6. What changes are needed in your main application (the test application here) to get it to work with the library? What about the compiling process?

  7. How many files does your library consist of? What are they? Which one(s) do you #include?

Handing it in...

After you've placed your TPQ answers in a separate file named like your main program file, you'll need to run your script. Don't forget to execute your program as many times as you need to show a good sample of testing your program! Finally, if you want/need corrections, go to your favorite email and send me the PDF with a proper subject.

This assignment is (Level 2).

Options


Last modified 02/12/2018 03:24:27.

It is now 3/15/2025, 6:36:22 AM.

Date you last viewed this page: That's for you to know and ...well, that's for you to know.

© 1993-2025  Jason James   (email — craie@acm.org — for permissions &/or details)