Ada Tutorial - Chapter 11

THE CHARACTER AND STRING TYPE

A QUICK REVIEW OF THE CHARACTER TYPE

Example program ------> e_c11_p1.ada

The best way to study any topic is with an example, so examine the program named e_c11_p1.ada for some examples using 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 Annex A.1 of the Ada 95 Reference Manual (ARM) 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 literal to which the variable named Another is initialized. A different literal 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 Annex K of the ARM.

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

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

THE STRING TYPE

Example program ------> e_c11_p2.ada

The program named e_c11_p2.ada illustrates some of the operations that can be done with the 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 e_c11_p2.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.

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 literal in line 21 is simply another string constant, but it does not have a name. Finally, we assign data to 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 instead of the two separate output procedure calls in lines 30 and 31, 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.

CONCATENATION OF STRINGS

Example program ------> e_c11_p3.ada

Examine the program e_c11_p3.ada for several examples of string concatenation. Two 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 Ada.Characters.Latin_1, 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

Example program ------> e_c11_p4.ada

The example program named e_c11_p4.ada will give you some examples of string comparisons as used 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.

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

Example program ------> e_c11_p5.ada

Examine the program named e_c11_p5.ada for examples of how you can convert from CHARACTER 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.

THERE ARE TWO KINDS OF STRINGS NOW

With the upgrade to Ada 95, there are now two kinds of strings. The STRING type that we have been discussing in this chapter, and a new WIDE_STRING type. Since there are far more than 256 different characters in some languages around the world, and Ada is approved by the International Standards Organization (ISO), it was necessary to provide the ability to handle many more characters. The WIDE_CHARACTER type was defined which provides 65,536 different characters, and the WIDE_STRING library was provided which uses the larger character type for each of its characters. The first 256 characters of the WIDE_CHARACTER type are the same as the characters in the CHARACTER type. The remaining characters can be defined as needed for whatever language is being used in any given application.

A NEW, VERY USEFUL LIBRARY PACKAGE

Ada 95 has a new character handling library defined for use in text based processing. The library named Ada.Characters.Handling is composed of many useful subprograms for use with text handling. It contains, for example, a function named Is_Upper which returns a BOOLEAN value indicating whether the character passed in as a parameter is upper case or not. Another function, To_Upper changes the case of the character passed in to upper case, if it is an alphabetic character. There are functions to check for whitespace, if a character is numeric, if it is a special character, and many other useful functions. The student is encouraged to study this package provided by your compiler, especially if text processing will be a major part of your programming efforts.

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.

Ada 95 has an improvement that was not available with Ada 83, the predefined string packages which will be covered later in this tutorial.

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.(Solution)
  2. Add code to e_c11_p5.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.(Solution)
Advance to Chapter 12

Return to the Table of Contents


Copyright © 1988-1998 Coronado Enterprises - Last update, February 1, 1998
Gordon Dodrill - dodrill@swcp.com - Please email any comments or suggestions.