08-11-2012, 01:08 PM
Data Structures Using C
1Data Structures.pdf (Size: 171.82 KB / Downloads: 55)
Strings
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 aesthetically pleasing to you and the
people using the output of your program, you need the ability to output
text data. We have used strings extensively already, without actually
defining them. A complete definition of a string is ‘a sequence of char
type data terminated by a NULL character,’.
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
character (which is usually a character with a zero ASCII code number)
is detected. You should also recall (from Module 813: Fundamental
Programming Structures in C) that the char type is really a special form
of integer – one that stores the ASCII code numbers which represent
characters and symbols.
An array (as we shall discover shortly) is a series of homogeneous
pieces of data that are all identical in type. The data type can be quite
complex as we will see when we get to the section of this module
discussing structures. A string is simply a special case of an array, an
array of char type data. The best way to see these principles is by use of
an example [CHRSTRG.C].
Using strings
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 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 statement which sets
NULL equal to zero would allow us to use NULL instead of an actual
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.
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].
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.
Some string subroutines
The next example [STRINGS.C] illustrates a few of the more common
string handling functions. These functions are found in the C standard
library and are defined in the header file string.h.
Alphabetical sorting of strings
The next function to be considered is the strcmp or the string compare
function. It will return a 1 if the first string is lexicographically larger
than the second, zero if they are the two strings are identical, and -1 if
the first string is lexicographically smaller than the second. A
lexicographical comparison uses the ASCII codes of the characters as
the basis for comparison. Therefore, ‘A’ will be “smaller” than ‘Z’
because the ASCII code for ‘A’ is 65 whilst the code for ‘Z’ is 90.
Sometimes however, strange results occur. A string that begins with the
letter ‘a’ is larger than a string that begins with the letter ‘Z’ since the
ASCII code for ‘a’ is 97 whilst the code for ‘Z’ is 90.
Combining strings
The last four statements in the [STRINGS.C] example have another
new feature, the strcat, or string concatenation function. This function
simply adds the characters from one string onto the end of another
string taking care to adjust the NULL so a single string is produced. In
this case, name1 is copied into mixed, then two blanks are concatenated
to mixed, and finally name2 is concatenated to the combination. The
result is printed out which shows both names stored in the one variable
called mixed.