home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / pascal / PAS_TUT.ZIP / CHAP07.TXT < prev    next >
Encoding:
Text File  |  1991-02-04  |  6.1 KB  |  177 lines

  1.  
  2.  
  3.                                                         Chapter 7
  4.                                       STRINGS & STRING PROCEDURES
  5.  
  6. PASCAL STRINGS
  7. _________________________________________________________________
  8.  
  9. According to the Pascal definition, a string is  ================
  10. simply an array of 2 of more elements of type      STRARRAY.PAS
  11. char, and is contained in an array defined in a  ================
  12. var declaration as a fixed length.  Examine the
  13. example program named STRARRAY.PAS.  Notice that
  14. the strings are defined in the type declaration even though they
  15. could have been defined in the var part of the declaration.  This
  16. is to begin getting you used to seeing the type declaration.  The
  17. strings defined here are nothing more than arrays with char type
  18. variables.
  19.  
  20.  
  21. A STRING IS AN ARRAY OF CHAR
  22. _________________________________________________________________
  23.  
  24. The interesting part of this file is the executable program. 
  25. Notice that when the variable First_Name is assigned a value, the
  26. value assigned to it must contain exactly 10 characters or the
  27. compiler will generate an error.  If you edit out a blank, you will
  28. get an invalid type error.  Pascal is neat in allowing you to write
  29. out the values in the string array without specifically writing
  30. each character in a loop as can be seen in the Writeln statement. 
  31. To combine the data, called concatenation, requires the use of the
  32. rather extensive looping and subscripting seen in the last part of
  33. the program.  It would be even messier if we were to consider
  34. variable length fields which is nearly always the case in a real
  35. program.
  36.  
  37. Two things should be observed in this program.  First, notice the
  38. fact that the string operations are truly array operations and will
  39. follow all of the characteristics discussed in the last chapter. 
  40. Secondly, it is very obvious that Pascal is rather weak when it
  41. comes to its handling of text type data.  Pascal will handle text
  42. data, even though it may be difficult to do so using the standard
  43. description of Pascal as illustrated in this program.  We will see
  44. next that TURBO Pascal really shines when it is desired to
  45. manipulate text.  Compile and run STRARRAY.PAS and observe the
  46. output.
  47.  
  48.  
  49. THE TURBO PASCAL STRING TYPE
  50. _________________________________________________________________
  51.  
  52. Examine the example program STRINGS.PAS.  You     ===============
  53. will see a much more concise program that           STRINGS.PAS
  54. actually does more.  TURBO Pascal has, as an      ===============
  55. extension to standard Pascal, the string type of
  56. variable.  It is used as shown, and the number
  57.  
  58.                                                          Page 7-1
  59.  
  60.                         Chapter 7 - Strings and String Procedures
  61.  
  62. in the square brackets in the var declaration is the maximum length
  63. of the string.  In actual use in the program, the variable can be
  64. used as any length from zero characters up to the maximum given in
  65. the declaration.  The variable First_Name, for example, actually
  66. has 11 locations of storage for its data.  The current length is
  67. stored in First_Name[0] and the data is stored in First_Name[1]
  68. through First_Name[10].  All data are stored as byte variables,
  69. including the size, so the length is limited to a maximum of 255
  70. characters.
  71.  
  72.  
  73. STRINGS HAVE VARIABLE LENGTHS
  74. _________________________________________________________________
  75.  
  76. Now look at the program itself.  Even though the variable
  77. First_Name is defined as 10 characters long, it is perfectly legal
  78. to assign it a 4 character constant, with First_Name[0]
  79. automatically set to 4 by the system and the last six characters
  80. undefined and unneeded.  When the program is run, the three
  81. variables are printed out all squeezed together indicating that the
  82. variables are indeed shorter than their full size as defined in the
  83. var declaration.  
  84.  
  85. Using the string type is even easier when you desire to combine
  86. several fields into one as can be seen in the assignment to
  87. Full_Name.  The concatenation operator is the plus sign and is used
  88. to combine strings and individual characters as indicated in line
  89. 14.  Notice that there are even two blanks, in the form of constant
  90. fields, inserted between the component parts of the full name. 
  91. When it is written out, the full name is formatted neatly and is
  92. easy to read.  Compile and run STRINGS.PAS and observe the output.
  93.  
  94.  
  95. WHAT'S IN A STRING TYPE VARIABLE?
  96. _________________________________________________________________
  97.  
  98. The next example program named WHATSTRG.PAS, is  ================
  99. intended to show you exactly what is in a string   WHATSTRG.PAS
  100. variable.  This program is identical to the last ================
  101. program except for some added statements at the
  102. end.  Notice the assignment to Total.  The
  103. function Length is available in TURBO Pascal to return the current
  104. length of any string type variable.  It returns a byte type
  105. variable with the value contained in the [0] position of the
  106. variable.  We print out the number of characters in the string at
  107. this point, and then print out each character on a line by itself
  108. to illustrate that the TURBO Pascal string type variable is simply
  109. an array variable.
  110.  
  111. The TURBO Pascal reference manual has a full description of several
  112. more procedures and functions for use with strings which are
  113. available in TURBO Pascal only.  Refer to your TURBO Pascal
  114. reference manual for complete details.  The use of these should be
  115. clear after you grasp the material covered here.
  116.  
  117.                                                          Page 7-2
  118.  
  119.                         Chapter 7 - Strings and String Procedures
  120.  
  121.  
  122. PROGRAMMING EXERCISE
  123. _________________________________________________________________
  124.  
  125. 1.   Write a program in which you store your first, middle, and
  126.      last names as variables, then display them one to a line. 
  127.      Concatenate the names with blanks between them and display
  128.      your full name as a single variable.
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.                                                          Page 7-3
  177.