Programming in C

(Barry) #1

198 Chapter 10 Character Strings


The first forloop inside the concatfunction copies the characters from the str1array
into the resultarray.This loop is executed n1times, which is the number of characters
contained inside the str1array.
The second forloop copies str2into the resultarray. Because str1was n1charac-
ters long, copying into resultbegins at result[n1]—the position immediately follow-
ing the one occupied by the last character of str1.After this forloop is done, the
resultarray contains the n1+n2characters representing str2concatenated to the end of
str1.
Inside the mainroutine, two const characterarrays,s1and s2,are defined.The
first array is initialized to the characters 'T','e','s','t',and ' '.This last character
represents a blank space and is a perfectly valid character constant.The second array is
initially set to the characters 'w','o','r','k','s',and '.'.A third character array,s3,
is defined with enough space to hold s1concatenated to s2, or 11 characters. It is not
declared as a constarray because its contents will be changed.
The function call
concat (s3, s1, 5, s2, 6);
calls the concatfunction to concatenate the character arrays s1and s2, with the desti-
nation array s3.The arguments 5 and 6 are passed to the function to indicate the num-
ber of characters in s1and s2,respectively.
After the concatfunction has completed execution and returns to main,a forloop is
set up to display the results of the function call.The 11 elements of s3are displayed at
the terminal, and as can be seen from the program’s output, the concatfunction seems
to be working properly. In the preceding program example, it is assumed that the first
argument to the concatfunction—the result array—contains enough space to hold the
resulting concatenated character arrays. Failure to do so can produce unpredictable results
when the program is run.

Va r iable-Length Character Strings


You can adopt a similar approach to that used by the concatfunction for defining other
functions to deal with character arrays.That is, you can develop a set of routines, each of
which has as its arguments one or more character arrays plus the number of characters
contained in each such array. Unfortunately, after working with these functions for a
while, you will find that it gets a bit tedious trying to keep track of the number of char-
acters contained in each character array that you are using in your program—especially if
you are using your arrays to store character strings of varying sizes.What you need is a
method for dealing with character arrays without having to worry about precisely how
many characters you have stored in them.
There is such a method, and it is based upon the idea of placing a special character at
the end of every character string. In this manner, the function can then determine for
itself when it has reached the end of a character string after it encounters this special
character. By developing all of your functions to deal with character strings in this
Free download pdf