


                                                       Chapter 11
                                    THE CHARACTER AND STRING TYPE


A QUICK REVIEW OF THE CHARACTER TYPE
_________________________________________________________________

The best way to study any topic is with an        ===============
example, so examine the program named CHARS.ADA      CHARS.ADA
for some examples using the CHARACTER type        ===============
variables.

The type CHARACTER is a predefined type in Ada and is defined as
the printable set of ASCII characters including a few that don't
actually print.  See appendix C of the LRM for a complete list of
the CHARACTER elements.  All of the operations available with the
enumerated type variable are available with the CHARACTER type
variable.  To illustrate their use, we declare two CHARACTER type
variables in lines 7 and 8 with the second being initialized to the
letter D.  Note the single quote marks which define the CHARACTER
type constant to which the variable named Another is initialized.
A different constant value is assigned to the variable My_Char in
line 12, and the two variables are compared  in the if statement.
Since 'A' is of lesser value than 'D', the line of text in line 14
is output to the monitor.

Lines 17 through 20 display some very predictable output that is
included as an example of CHARACTER output, and finally some of the
attributes available with the CHARACTER type variable are
illustrated in lines 22 through 24.  The same attributes are
defined for the CHARACTER type variable as for the enumerated type
and all are listed in appendix A of the LRM.

Compile and execute this program to get a feel for use of the
CHARACTER type variable.

You may wish to review the program named MOREDERS.ADA in chapter
7 to refresh your mind on declaring subtypes and derived types of
the predefined CHARACTER type.


THE STRING TYPE
_________________________________________________________________

The program named STRING1.ADA illustrates some    ===============
of the operations that can be done with the         STRING1.ADA
predefined type STRING.  A string is an array of  ===============
CHARACTER type variables which is of a fixed
length and starts with element number 1 or
higher.  The index uses type POSITIVE.  Note that this program is
called STRING1.ADA instead of the more desirable name of STRING.ADA
because the word STRING is a predefined word in Ada and using it
for the program name would make it unavailable for its correct use.

                                                        Page 11-1

                       Chapter 11 - The Character and String Type


Line 7 declares an uninitialized string of 33 characters, while
line 8 declares a constant string of four elements initialized to
the word "John", and illustrates rather graphically that the string
is composed of individual CHARACTER type elements.  Line 9 declares
another constant that is initialized, which all constants must be
in order to be useful.  Note that lines 8 and 9 did not contain a
character count, the computer counted the characters for us and
supplied the limits of the array.



DECLARING A STRING VARIABLE
_________________________________________________________________

Line 10 defines a STRING variable, which will be initialized to the
string given.  Even though the initialization string is given, the
array limits must be explicitly specified for a variable.  Not only
must the limits be given, the number of elements in the
initialization string must agree with the number of elements
defined as the array range, or a compiler error will be given.
This is the first difficulty encountered when using strings, but
there will be more as we progress.  It seems like the computer
should be able to count the characters in the variable for us, but
due to the strong type checking used in Ada, this cannot be done.



STRING MANIPULATION IS DIFFICULT
_________________________________________________________________

When we get to the executable part of the program, we assign a
string constant to the string variable named Line.  Once again,
according to the definition of Ada, the string constant must have
exactly the same number of characters as the number of characters
in the declaration of the variable Line, or a compile error will
be issued.  This is another seemingly unnecessary inconvenience in
the use of strings which we must put up with.  The variable named
Line is displayed on the monitor in line 18, and some of the other
constants are displayed along with it.  Note that the string
constant in line 21 is simply another string constant, but it does
not have a name.  Finally, we assign a few individual elements of
the string variable named Address in such a way to illustrate that
it is indeed an array, then do a slice assignment, and finally
output the result.  It should be noted that the Put_Line could be
used in lines 30 and 31 instead of the two separate output
procedure calls, but it is simply a matter of personal taste.

Compile and run this program and see that the output is exactly
what you predict from your understanding of the program.





                                                        Page 11-2

                       Chapter 11 - The Character and String Type

CONCATENATION OF STRINGS
_________________________________________________________________

Examine the program CONCAT.ADA for several       ================
examples of string concatenation.  Two              CONCAT.ADA
uninitialized string variables are declared in   ================
lines 7 and 8, and they are used throughout the
program.

Line 12 illustrates concatenation of a three element string and a
four element string by using the concatenation operator, the "&".
The four element string is appended to the end of the three element
string forming a seven element string which is assigned to the
variable String7.  Line 21 illustrates concatenation of a four
element variable with a three element constant.

Line 24 is the most interesting assignment here, because it is a
concatenation of four strings, two of which contain only one
element each.  The values of "CR" and "LF" are such that they
produce a "carriage return" and "line feed" when sent to the
monitor, so that when String7 is output, it will be on two
successive lines of the monitor.  The ASCII values of all of the
characters are available in the predefined package named ASCII,
which is why the dotted notation gives the actual value of these
constants.  Use of the dot notation in this manner will be more
fully defined later in this tutorial.  Be sure to compile and run
this program, and be sure you understand the results.



STRING COMPARISONS
_________________________________________________________________

The example program named STRNGCMP.ADA will give ================
you some examples of string comparisons as used    STRNGCMP.ADA
in Ada, so you should examine it at this time.   ================
The string declarations are nothing new to you,
so nothing more will be said about them.

In line 15 where the constants MY_CAR and YOUR_CAR are compared for
inequality, they will not be equal since the case is different for
some of the characters, and case matters in a string expression.
A different ASCII value is used for the letter 'A' than that used
for the letter 'a', so they are not the same.  For a string
comparison to be equal, all elements must be exactly the same as
the corresponding elements in the other string, and the number of
elements must be the same.  Therefore, following execution of line
19, the value assigned to Her_Car is still not the same as the
value stored in the constant MY_CAR.  If you attempted to compare
them, you would get a compile error because the two strings have
a different length, so they could never compare anyway.  Line 24
illustrates that a variable can be compared to a string literal.



                                                        Page 11-3

                       Chapter 11 - The Character and String Type

Lines 20 through 22 are examples of legal statements according to
the Ada definition.  Compile and run this program and study the
resulting output.


ATTRIBUTES OF CHARACTERS AND STRINGS
_________________________________________________________________

Examine the program named CHARINT.ADA for         ===============
examples of how you can convert from CHARACTER      CHARINT.ADA
type variables to INTEGER type variables and      ===============
back.  The attributes POS and VAL are used as
shown.  In order to increment a character, for
example an 'A', to the next value, it must be converted to INTEGER,
incremented, then converted back to CHARACTER.  Of course you could
always use the SUCC attribute to increment the CHARACTER type
variable.

This program should be self explanatory.  after you study it,
compile and run it.


DYNAMIC STRINGS ARE COMING
_________________________________________________________________

You may not feel too good about the use of strings in Ada because
of the lack of flexibility, but don't worry about them.  Ada was
written to be an extendable language and when we get to chapter 16,
we will have an example package that will give you the ability to
use strings the way you would like to.  A rather extensive dynamic
string package will be presented to you and you will have the
ability to refine it even further if you so desire.  In effect, you
will have the ability to extend the Ada language.


PROGRAMMING EXERCISES
_________________________________________________________________

1.   Write a program to declare your first name as a STRING
     constant, and your last name as another STRING constant.
     Concatenate your first and last names with a blank between
     them and display the result with a single Put_Line.  You will
     find much inflexibility in the definition of the STRING
     variable you use for the result.

2.   Add code to CHARINT.ADA to increment the variable named Char
     by using the POS and VAL attributes.  Then add additional code
     to increment the same variable through use of the SUCC
     attribute.




                                                        Page 11-4