TL13.TXT 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 60
  2. TURBO-LESSON 13: STRINGS
  3. OBJECTIVES - In this lesson, you will learn about:
  4. 1. Strings
  5. 2. String replacement statement
  6. 3. Predefined string function, LENGTH
  7. 1. Strings.
  8. You have already seen some Pascal strings in the WriteLn
  9. statements of earlier lessons.
  10. WriteLn('This is a string.');
  11. It is often convenient to store strings as variables or
  12. constants.
  13. A string constant may be defined in the CONST section:
  14. CONST String_1 = 'TURBO-LESSONS';
  15. String variables must be declared in the VAR section. The form
  16. of the declaration is:
  17. VAR First_Name : String[12];
  18. This sets up storage for a variable named First_Name which can
  19. store a string up to 12 characters long.
  20. 2. String replacement statement.
  21. The replacement statement for strings is:
  22. String_Name := (string expression);
  23. ##### DO:
  24. Examine PROG13. Notice the following:
  25. A string constant, S_Test is given the value 'Test String' in the
  26. CONST declaration section.
  27. Several string variables are defined in the VAR section.
  28. Œ
  29. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 61
  30. ##### DO:
  31. Run the program.
  32. What happens when an attempt is made to store too long a string
  33. in a string variable?
  34. ##### DO:
  35. Add the following to the program:
  36. S5 := S_Test;
  37. WriteLn(S5);
  38. Run the program.
  39. How many characters of S5 are printed?
  40. ##### DO:
  41. Modify WriteLn(S5) to:
  42. WriteLn('[', S5, ']');
  43. Run the program. How many characters of S5 are printed?
  44. DEBUGGING NOTE: When working with strings, you may find it
  45. helpful to print some kind of marker before and after a string to
  46. help "see" the occurrences of the character, blank.
  47. You have seen what happens when storing 'Test String' in too
  48. short a variable: S3 holds 'Tes', S8 holds 'Test Str'.
  49. What happens when a string is stored in a variable that is larger
  50. than needed? Are blanks added?
  51. ##### DO:
  52. Modify the WriteLn(S14) to bracket S14 (like you did above with
  53. S5) and run the program.
  54. How many characters of S14 were printed?
  55. Were extra blanks added? (More on this later.)
  56. Œ
  57. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 62
  58. ##### DO:
  59. Look at PROG13A.
  60. WriteLn(S8[I]);
  61. Notice the use of the square brackets in the statement above.
  62. This is a way to refer to a specific character in a string.
  63. S8[2] means the 2nd character in the string, S8.
  64. NOTE: SQUARE BRACKETS ARE USED IN TWO DIFFERENT WAYS WITH
  65. STRINGS. WHEN DECLARING VARIABLES, THE BRACKETS ENCLOSE THE
  66. MAXIMUM LENGTH OF THE STRING. IN PROCESSING STATEMENTS, THE
  67. NUMBER IN THE BRACKETS DESIGNATE A PARTICULAR CHARACTER IN A
  68. STRING.
  69. ##### DO:
  70. Run the program, using 2 as position number.
  71. Try 5 as an input. What character was stored as the 5th
  72. character of S8?
  73. The string stored in S8, 'TURBO', is 5 characters long
  74. but S8 is 8 characters long.
  75. What characters, if any, are stored in S8[6], S8[7], and
  76. S8[8]?
  77. ##### DO:
  78. Run the program with input values of 6, 7, and 8.
  79. What characters were printed?
  80. ##### DO:
  81. Add the following statement as the first statement in the BEGIN
  82. END block:
  83. S8 := '12345678';
  84. Run the program again, using 6, 7, and 8 as input.
  85. What do you conclude about "unused" positions in a string?
  86. Before you are prompted to enter the "position number", S8 is
  87. printed.
  88. Do positions 6, 7, 8 of the string print? Why?
  89. Œ
  90. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 63
  91. ##### DO:
  92. Run the program once more, this time use 0 as the input value.
  93. What character printed?
  94. If you look up this character, clubs symbol, in the ASCII chart
  95. in your BASIC manual, you find that it is the character
  96. associated with ASCII value 8.
  97. String variables in TURBO are one character longer than the
  98. maximum length you specify. This extra character is at the
  99. beginning of the string, at position 0, and always contains the
  100. length of the string stored.
  101. So why isn't the length stored as a number?
  102. Storing the length information as a character makes position 0
  103. the same type as the other characters in the string.
  104. 3. Predefined string function, LENGTH.
  105. Because the length of a string is often needed in processing,
  106. the function, LENGTH, has been provided for that purpose.
  107. ##### DO:
  108. Add the following statement after the UNTIL statement:
  109. WriteLn('Length of string: ', LENGTH(S8) );
  110. Run the program.
  111. There is also another way to get the ASCII value of the character
  112. at position 0 of a string.
  113. ##### DO:
  114. Add the following statement just before the END:
  115. WriteLn('ASCII value: ', ORD(S8[0]) );
  116. Run the program.
  117. Does the ASCII value agree with the value obtained with the
  118. LENGTH function?
  119. Œ
  120. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 64
  121. ##### DO:
  122. Just before the END, insert:
  123. FOR I := 0 to LENGTH(S8) DO
  124. WriteLn('Position ', I:2, ': ',S8[I]);
  125. Run the program.
  126. ##### DO:
  127. Change the CONST declaration to:
  128. S_Test = 'OK';
  129. Run the program.
  130. Also try 'Wake Up' for S_Test.
  131. Œ
  132.