home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / PASCTUT1.ZIP / CHAP7.TXT < prev    next >
Encoding:
Text File  |  1986-07-14  |  6.0 KB  |  125 lines

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