home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / pascal1.zip / CHAP7.TXT < prev    next >
Text File  |  1988-01-15  |  7KB  |  191 lines

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