Art Show | Course Home | Homework | Labs | Programming | Schedule & Lecture Notes | Submit

Saint Louis University

Computer Science P125
Introduction to Computer Science

Michael Goldwasser

Spring 2005

Dept. of Math & Computer Science

Programming Assignment 07

Finding it Old-School

Due: Friday, 29 April 2005, 8pm

Please see the general programming webpage for details about the programming environment for this course, guidelines for programming style, and details on electronic submission of assignments.

Collaboration Policy

For this assignment, you must work individually in regard to the design and implementation of your project.

Please make sure you adhere to the policies on academic integrity in this regard.


Contents:


Overview

The C++ string class is a very useful one indeed. It offers a very convenient way to represent a sequence of characters as well as many useful associated utility methods. One such method that we have already discussed is the method find which was used to find the leftmost occurrence of a pattern in an existing string, even when constrained to the portion of the existing string starting at a given index. If no such occurrence exists, this is denoted by a special return value.

Of course there was a time before the C++ string class existed, more notably before the language C++ existed. The C++ language is an extension of an earlier language known as C. In C, character strings are represented more directly as an array of characters. In fact, even in C++ the writers of the string class still use an array of characters behind-the-scenes. These are typical referred to as C-style strings in contrast, and are discussed in our text in Chapters 2.7.1 and 9.2.4.

In this assignment, we are going to have you write code to implement a routine such as find, but directly using an array of characters to represent the original string and the pattern to be found. This should give you a taste of what it would be like to live without the C++ string class (or to have been an original designer of that class).


Your Task

Your task will be to implement a function with the following syntax and semantics:

int find(char text[], int textLength, char pattern[], int patternLength, int startIndex)
Your goal is to find the first occurrence of the pattern starting at an index greater than or equal to startIndex in the given text. You are to return the starting index in the text where such a pattern begins. In the case where no such occurrence exists, you are to return the integer value -1.

For example, given the text: "Welcome to computer science" and the pattern: "com" a correct implementation will perform as follows:

You are responsible for ensuring that your code works correctly with all legitimate inputs. Please make sure to spend ample time testing your program in a variety of settings.


A Driver

To aid you in testing, we will provide you with a main routine that prompts the user for a piece of text and a pattern, and then finds all occurrences of the pattern in the text based on a series of calls to your routine. Please keep in mind that your routine is only responsible for finding one occurrence per call.


Files You Will Need

You will need a single file find.cpp which contains our main routine and a prototype for the function which you must complete. You may either download the file via a browser, or else copy it directly to your current directory on turing with the command:

cp -Rp ~goldwasser/csp125/programs/find .


Files to Submit


Grading Standards

The assignment is worth 10 points.


Extra Credit (1 point)

coming soon...perhaps something about search and replace


Michael Goldwasser
CS-P125, Spring 2005
Last modified: Monday, 02 May 2005
Art Show | Course Home | Homework | Labs | Programming | Schedule & Lecture Notes | Submit