Previous: 11.3.2 Words
Up: 11.3 More Example Programs
Previous Page: 11.3.2 Words
Next Page: 11.4 Common Errors
In string manipulations, it is frequently necessary to find a substring of a string. A substring is a string that is part of another string. It can be parameterized by specifying where the substring starts and how long it is. Our next task is to write a program that finds a substring of a string at a given position and of a specified size.
SUBSTR: Read substring parameters. For each line of input, find the appropriate substring.
For example, consider the string:
Source string: This is a test string0
(The terminating NULL is shown explicitly). A substring of this string starting at index position 2 and containing 5 characters is:
Destination string: is is0
We will write a function to extract such a substring. The function must be passed several arguments: the source string (pointer), a destination string where the specified substring is to be copied (pointer), the starting position of the substring in the source string (integer), and the number of characters for the substring (integer).
It may or may not be possible for the function to extract the string. For example, if the starting position is outside the string, no substring can be extracted. We will assume that the function returns the destination string (pointer) if successful in extracting a string; otherwise, it returns a NULL pointer to indicate failure. We will also assume that the function will extract as many characters as possible upto the specified number. The function prototype should be:
STRING substr(STRING src, STRING dest, int startpos, int nchrs);
The parameter, src, points to the source string, and dest points to an array where the substring of src is to be copied. The next two arguments provide the starting position and the number of characters. The calling function must allocate memory for the destination string. The starting position, startpos is an index into the array - it must be 0 or greater. The parameter, nchrs is the maximum number of characters to copy into the substring.
Since the program depends primarily on substr(), let us first develop an algorithm for it. The function must start copying characters from the starting position startpos. If we use an array index, src[startpos] accesses the character at the start position if startpos is in the source string. If startpos is not in the source string, we will return a NULL to indicate failure to extract a substring.
Next, we must copy up to a maximum of nchrs characters into dest. When the source string is exhausted or nchrs characters are copied, we stop the copy process and append a NULL to the substring. If even one character is copied into the substring, we will return the destination pointer. Here is the algorithm:
if startpos >= strlen(src)
return NULL
j = 0;
while j is less than nchrs and src is not exhausted
copy a character: dest[j] = src[startpos + j]
increment j
terminate dest with NULL
return dest
The program driver reads the start position and the number of characters. It
then reads strings until end of file and finds the substring for each string if
possible. The code for the driver and substr() is shown in Figure
11.15.
The program prints the substring if it can be extracted; otherwise, it prints a message. Here is a sample session: