home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog1 / chap11.txt < prev    next >
Encoding:
Text File  |  1991-07-01  |  10.2 KB  |  233 lines

  1.  
  2.  
  3.  
  4.                                                        Chapter 11
  5.                                     THE CHARACTER AND STRING TYPE
  6.  
  7.  
  8. A QUICK REVIEW OF THE CHARACTER TYPE
  9. _________________________________________________________________
  10.  
  11. The best way to study any topic is with an        ===============
  12. example, so examine the program named CHARS.ADA      CHARS.ADA
  13. for some examples using the CHARACTER type        ===============
  14. variables.
  15.  
  16. The type CHARACTER is a predefined type in Ada and is defined as
  17. the printable set of ASCII characters including a few that don't
  18. actually print.  See appendix C of the LRM for a complete list of
  19. the CHARACTER elements.  All of the operations available with the
  20. enumerated type variable are available with the CHARACTER type
  21. variable.  To illustrate their use, we declare two CHARACTER type
  22. variables in lines 7 and 8 with the second being initialized to the
  23. letter D.  Note the single quote marks which define the CHARACTER
  24. type constant to which the variable named Another is initialized.
  25. A different constant value is assigned to the variable My_Char in
  26. line 12, and the two variables are compared  in the if statement.
  27. Since 'A' is of lesser value than 'D', the line of text in line 14
  28. is output to the monitor.
  29.  
  30. Lines 17 through 20 display some very predictable output that is
  31. included as an example of CHARACTER output, and finally some of the
  32. attributes available with the CHARACTER type variable are
  33. illustrated in lines 22 through 24.  The same attributes are
  34. defined for the CHARACTER type variable as for the enumerated type
  35. and all are listed in appendix A of the LRM.
  36.  
  37. Compile and execute this program to get a feel for use of the
  38. CHARACTER type variable.
  39.  
  40. You may wish to review the program named MOREDERS.ADA in chapter
  41. 7 to refresh your mind on declaring subtypes and derived types of
  42. the predefined CHARACTER type.
  43.  
  44.  
  45. THE STRING TYPE
  46. _________________________________________________________________
  47.  
  48. The program named STRING1.ADA illustrates some    ===============
  49. of the operations that can be done with the         STRING1.ADA
  50. predefined type STRING.  A string is an array of  ===============
  51. CHARACTER type variables which is of a fixed
  52. length and starts with element number 1 or
  53. higher.  The index uses type POSITIVE.  Note that this program is
  54. called STRING1.ADA instead of the more desirable name of STRING.ADA
  55. because the word STRING is a predefined word in Ada and using it
  56. for the program name would make it unavailable for its correct use.
  57.  
  58.                                                         Page 11-1
  59.  
  60.                        Chapter 11 - The Character and String Type
  61.  
  62.  
  63. Line 7 declares an uninitialized string of 33 characters, while
  64. line 8 declares a constant string of four elements initialized to
  65. the word "John", and illustrates rather graphically that the string
  66. is composed of individual CHARACTER type elements.  Line 9 declares
  67. another constant that is initialized, which all constants must be
  68. in order to be useful.  Note that lines 8 and 9 did not contain a
  69. character count, the computer counted the characters for us and
  70. supplied the limits of the array.
  71.  
  72.  
  73.  
  74. DECLARING A STRING VARIABLE
  75. _________________________________________________________________
  76.  
  77. Line 10 defines a STRING variable, which will be initialized to the
  78. string given.  Even though the initialization string is given, the
  79. array limits must be explicitly specified for a variable.  Not only
  80. must the limits be given, the number of elements in the
  81. initialization string must agree with the number of elements
  82. defined as the array range, or a compiler error will be given.
  83. This is the first difficulty encountered when using strings, but
  84. there will be more as we progress.  It seems like the computer
  85. should be able to count the characters in the variable for us, but
  86. due to the strong type checking used in Ada, this cannot be done.
  87.  
  88.  
  89.  
  90. STRING MANIPULATION IS DIFFICULT
  91. _________________________________________________________________
  92.  
  93. When we get to the executable part of the program, we assign a
  94. string constant to the string variable named Line.  Once again,
  95. according to the definition of Ada, the string constant must have
  96. exactly the same number of characters as the number of characters
  97. in the declaration of the variable Line, or a compile error will
  98. be issued.  This is another seemingly unnecessary inconvenience in
  99. the use of strings which we must put up with.  The variable named
  100. Line is displayed on the monitor in line 18, and some of the other
  101. constants are displayed along with it.  Note that the string
  102. constant in line 21 is simply another string constant, but it does
  103. not have a name.  Finally, we assign a few individual elements of
  104. the string variable named Address in such a way to illustrate that
  105. it is indeed an array, then do a slice assignment, and finally
  106. output the result.  It should be noted that the Put_Line could be
  107. used in lines 30 and 31 instead of the two separate output
  108. procedure calls, but it is simply a matter of personal taste.
  109.  
  110. Compile and run this program and see that the output is exactly
  111. what you predict from your understanding of the program.
  112.  
  113.  
  114.  
  115.  
  116.  
  117.                                                         Page 11-2
  118.  
  119.                        Chapter 11 - The Character and String Type
  120.  
  121. CONCATENATION OF STRINGS
  122. _________________________________________________________________
  123.  
  124. Examine the program CONCAT.ADA for several       ================
  125. examples of string concatenation.  Two              CONCAT.ADA
  126. uninitialized string variables are declared in   ================
  127. lines 7 and 8, and they are used throughout the
  128. program.
  129.  
  130. Line 12 illustrates concatenation of a three element string and a
  131. four element string by using the concatenation operator, the "&".
  132. The four element string is appended to the end of the three element
  133. string forming a seven element string which is assigned to the
  134. variable String7.  Line 21 illustrates concatenation of a four
  135. element variable with a three element constant.
  136.  
  137. Line 24 is the most interesting assignment here, because it is a
  138. concatenation of four strings, two of which contain only one
  139. element each.  The values of "CR" and "LF" are such that they
  140. produce a "carriage return" and "line feed" when sent to the
  141. monitor, so that when String7 is output, it will be on two
  142. successive lines of the monitor.  The ASCII values of all of the
  143. characters are available in the predefined package named ASCII,
  144. which is why the dotted notation gives the actual value of these
  145. constants.  Use of the dot notation in this manner will be more
  146. fully defined later in this tutorial.  Be sure to compile and run
  147. this program, and be sure you understand the results.
  148.  
  149.  
  150.  
  151. STRING COMPARISONS
  152. _________________________________________________________________
  153.  
  154. The example program named STRNGCMP.ADA will give ================
  155. you some examples of string comparisons as used    STRNGCMP.ADA
  156. in Ada, so you should examine it at this time.   ================
  157. The string declarations are nothing new to you,
  158. so nothing more will be said about them.
  159.  
  160. In line 15 where the constants MY_CAR and YOUR_CAR are compared for
  161. inequality, they will not be equal since the case is different for
  162. some of the characters, and case matters in a string expression.
  163. A different ASCII value is used for the letter 'A' than that used
  164. for the letter 'a', so they are not the same.  For a string
  165. comparison to be equal, all elements must be exactly the same as
  166. the corresponding elements in the other string, and the number of
  167. elements must be the same.  Therefore, following execution of line
  168. 19, the value assigned to Her_Car is still not the same as the
  169. value stored in the constant MY_CAR.  If you attempted to compare
  170. them, you would get a compile error because the two strings have
  171. a different length, so they could never compare anyway.  Line 24
  172. illustrates that a variable can be compared to a string literal.
  173.  
  174.  
  175.  
  176.                                                         Page 11-3
  177.  
  178.                        Chapter 11 - The Character and String Type
  179.  
  180. Lines 20 through 22 are examples of legal statements according to
  181. the Ada definition.  Compile and run this program and study the
  182. resulting output.
  183.  
  184.  
  185. ATTRIBUTES OF CHARACTERS AND STRINGS
  186. _________________________________________________________________
  187.  
  188. Examine the program named CHARINT.ADA for         ===============
  189. examples of how you can convert from CHARACTER      CHARINT.ADA
  190. type variables to INTEGER type variables and      ===============
  191. back.  The attributes POS and VAL are used as
  192. shown.  In order to increment a character, for
  193. example an 'A', to the next value, it must be converted to INTEGER,
  194. incremented, then converted back to CHARACTER.  Of course you could
  195. always use the SUCC attribute to increment the CHARACTER type
  196. variable.
  197.  
  198. This program should be self explanatory.  after you study it,
  199. compile and run it.
  200.  
  201.  
  202. DYNAMIC STRINGS ARE COMING
  203. _________________________________________________________________
  204.  
  205. You may not feel too good about the use of strings in Ada because
  206. of the lack of flexibility, but don't worry about them.  Ada was
  207. written to be an extendable language and when we get to chapter 16,
  208. we will have an example package that will give you the ability to
  209. use strings the way you would like to.  A rather extensive dynamic
  210. string package will be presented to you and you will have the
  211. ability to refine it even further if you so desire.  In effect, you
  212. will have the ability to extend the Ada language.
  213.  
  214.  
  215. PROGRAMMING EXERCISES
  216. _________________________________________________________________
  217.  
  218. 1.   Write a program to declare your first name as a STRING
  219.      constant, and your last name as another STRING constant.
  220.      Concatenate your first and last names with a blank between
  221.      them and display the result with a single Put_Line.  You will
  222.      find much inflexibility in the definition of the STRING
  223.      variable you use for the result.
  224.  
  225. 2.   Add code to CHARINT.ADA to increment the variable named Char
  226.      by using the POS and VAL attributes.  Then add additional code
  227.      to increment the same variable through use of the SUCC
  228.      attribute.
  229.  
  230.  
  231.  
  232.  
  233.                                                         Page 11-4