CSC 101 Lab Week 6
A String Function




ISSUED: Wednesday, 2 May 2012
DUE: Wednesday, 9 May 2012, by the end of lab
POINTS POSSIBLE: 1
WEIGHT: 1% of total class grade

Specification

Write a function named substr that takes two string parameters and returns the number of times that the first string appears in the second string. For example, substr("xx", "abcxxabxxa") returns 2; substr("xx", "abcdefg") returns 0.

The function scans for the start of substring occurrences one character at a time. So for example, substr("xx", "abcxxxa") returns 2 and substr("xx", "abcxxxxa") returns 3.

Write a main function that prompts for two strings of up to 80 characters each and reads the strings from standard input. It then calls the substr function and prints the result. Name the program with the substr and main functions substr.c, and compile with

gcc -ansi -pedantic -Wall -Werror -g substr.c -o substr

Three sample runs of the program look like this:

unix1> substr
Input two strings up to 80 chars each: xx abcxxabxxa
The string "xx" appears 2 times in the string "abcxxabxxa"

unix1> substr
Input two strings up to 80 chars each: xx abcdefg
The string "xx" appears 0 times in the string "abcdefg"

unix1> substr
Input two strings up to 80 chars each: xx abcxxxxa
The string "xx" appears 3 times in the string "abcxxxxa"

Relevance to the Upcoming Midterm

One or more of the programming questions on the May 11th midterm will be similar to this lab. That is, you will be asked to write programs that involve one or more of the following concepts:

Submitting Your Work

Sometime before end of lab on Wednesday May 11, demonstrate that your programs runs correctly. To verify that you've completed the lab, submit your work as follows:

handin gfisher 101_lab6 substr.c