home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
257_01
/
chap7.txt
< prev
next >
Wrap
Text File
|
1988-03-30
|
19KB
|
455 lines
Chapter 7 - Strings and Arrays
WHAT IS A STRING?
A string is a group of characters, usually letters of
the alphabet. In order to format your printout in such a
way that it looks nice, has meaningful titles and names, and
is esthetically pleasing to you and the people using the
output of your program, you need the ability to output text
data. Actually you have already been using strings, because
the second program in this tutorial, way back in Chapter 2,
output a message that was handled internally as a string. A
complete definition is a series of "char" type data
terminated by a NULL character, which is a zero.
When C is going to use a string of data in some way,
either to compare it with another, output it, copy it to
another string, or whatever, the functions are set up to do
what they are called to do until a NULL, which is a zero, is
detected.
WHAT IS AN ARRAY?
An array is a series of homogeneous pieces of data that
are all identical in type, but the type can be quite complex
as we will see when we get to the chapter of this tutorial
discussing structures. A string is simply a special case of
an array, a series of char type data.
The best way to see these principles is by use of an
example, so load the program CHRSTRG.C and display it on
your monitor. The first thing new is the line that defines
a "char" type of data entity. The square brackets define an
array subscript in C, and in the case of the data definition
statement, the 5 in the brackets defines 5 data fields of
type "char" all defined as the variable "name". In the C
language, all subscripts start at 0 and increase by 1 each
step up to the maximum which in this case is 4. We
therefore have 5 "char" type variables named, "name[0]",
"name[1]", "name[2]", "name[3]", and "name[4]". You must
keep in mind that in C, the subscripts actually go from 0 to
one less than the number defined in the definition
statement. This is due to the original definition of C and
these limits cannot be changed or redefined by the
programmer.
HOW DO WE USE THE STRING?
The variable "name" is therefore a string which can
hold up to 5 characters, but since we need room for the NULL
terminating character, there are actually only four useful
characters. To load something useful into the string, we
have 5 statements, each of which assigns one alphabetical
Page 46
Chapter 7 - Strings and Arrays
character to one of the string characters. Finally, the
last place in the string is filled with the numeral 0 as the
end indicator and the string is complete. (A "define" would
allow us to use "NULL" instead of a zero, and this would add
greatly to the clarity of the program. It would be very
obvious that this was a NULL and not simply a zero for some
other purpose.) Now that we have the string, we will simply
print it out with some other string data in the output
statement.
The %s is the output definition to output a string and
the system will output characters starting with the first
one in "name" until it comes to the NULL character, and it
will quit. Notice that in the "printf" statement, only the
variable name "name" needs to be given, with no subscript
since we are interested in starting at the beginning.
(There is actually another reason that only the variable
name is given without brackets. The discussion of that
topic will be given in the next chapter.)
OUTPUTTING PART OF A STRING
The next "printf" illustrates that we can output any
single character of the string by using the "%c" and naming
the particular character of "name" we want by including the
subscript. The last "printf" illustrates how we can output
part of the string by stating the starting point by using a
subscript. The & specifies the address of "name[1]". We
will study this in the next chapter but I thought you would
benefit from a little glimpse ahead.
This example may make you feel that strings are rather
cumbersome to use since you have to set up each character
one at a time. That is an incorrect conclusion because
strings are very easy to use as we will see in the next
example program.
Compile and run this program.
SOME STRING SUBROUTINES
Load the example program STRINGS.C for an example of
some ways to use strings. First we define four strings.
Next we come to a new function that you will find very
useful, the "strcpy" function, or string copy. It copies
from one string to another until it comes to the NULL
character. Remember that the NULL is actually a "0" and is
added to the character string by the system. It is easy to
remember which one gets copied to which if you think of them
like an assignment statement. Thus if you were to say, for
Page 47
Chapter 7 - Strings and Arrays
example, "x = 23;", the data is copied from the right entity
to the left one. In the "strcpy" function, the data is also
copied from the right entity to the left, so that after
execution of the first statement, name1 will contain the
string "Rosalinda", but without the double quotes, they are
the compiler's way of knowing that you are defining a
string.
Likewise, "Zeke" is copied into "name2" by the second
statement, then the "title" is copied. The title and both
names are then printed out. Note that it is not necessary
for the defined string to be exactly the same size as the
string it will be called upon to store, only that it is at
least as long as the string plus one more character for the
NULL.
ALPHABETICAL SORTING OF STRINGS
The next function we will look at is the "strcmp" or
the string compare function. It will return a 1 if the
first string is larger than the second, zero if they are the
same length and have the same characters, and -1 if the
first string is smaller than the second. One of the
strings, depending on the result of the compare is copied
into the variable "mixed", and the largest name
alphabetically is printed out. It should come as no
surprise to you that "Zeke" wins because it is
alphabetically larger, length doesn't matter, only the
alphabet. It might be wise to mention that the result would
also depend on whether the letters were upper or lower case.
There are functions available with your C compiler to change
the case of a string to all upper or all lower case if you
desire. These will be used in an example program later in
this tutorial.
COMBINING STRINGS
The last four statements have another new feature, the
"strcat", or string conc