(c)opyright Brent M. Dingle 2001 Strings Brent M. Dingle Texas A&M University Chapter 9 – Section 3 (and some from Mastering Turbo Pascal 5.5, 3rd Edition by Tom Swan) Fall 2001 (c)opyright Brent M. Dingle 2001
(c)opyright Brent M. Dingle 2001 Strings We have actually been using strings for some time now. So you should be familiar with how they work. A couple things you might not have discovered yet: How to index into a string How to pass a string of declared size to a procedure or function. Fall 2001 (c)opyright Brent M. Dingle 2001
(c)opyright Brent M. Dingle 2001 Indexing into a string Suppose we had the following: VAR name : string[30]; : name := ‘Lilly Flower’; Now how might we determine what the first letter of name is? (say we were doing something alphabetically with names) Any guesses? Fall 2001 (c)opyright Brent M. Dingle 2001
Indexing into a string (cont) As it turns out strings are ‘special’ arrays in Pascal. So we can do some neat tricks with them by treating them as arrays. So the first character of name can be found by: Writeln(name[1]); Notice this means strings start their indexing at ONE. What do you think the value of name[5] is? Fall 2001 (c)opyright Brent M. Dingle 2001
(c)opyright Brent M. Dingle 2001 Name[k] values For reference: name[1] = ‘L’ name[2] = ‘i’ name[3] = ‘l’ name[4] = ‘l’ name[5] = ‘y’ name[6] = ‘ ‘ that’s a space character name[7] = ‘F’ name[8] = ‘l’ : : name[12] = ‘r’ Fall 2001 (c)opyright Brent M. Dingle 2001
(c)opyright Brent M. Dingle 2001 Strings to procedures As you undoubtedly have discovered from the homework, the procedure declaration: procedure GetName(var name : string[40]); Fails to compile. Previously the only fix was to just say: procedure GetName(var name : string); Fall 2001 (c)opyright Brent M. Dingle 2001
Strings to procs (cont) However, now we have learned that we can use TYPEs. So declare the type: TYPE string40 = string[40]; And use the declaration: procedure GetName(var name : string40); Fall 2001 (c)opyright Brent M. Dingle 2001
String and array passing Notice similar difficulties occur when we try to pass arrays: procedure La(var grades : array [1..5] of real); Does NOT work. But with items declared as below: TYPE GRADE_ARY = array [1..5] of real; : procedure La(var grades : GRADE_ARY); It does work and all is happy time. Fall 2001 (c)opyright Brent M. Dingle 2001
(c)opyright Brent M. Dingle 2001 String Functions Some useful string functions in Turbo Pascal (know them, use them, lov… well, just know and use them =) concat(str1, str2, …, str[n]); pos(find_me, in_this); delete(from_me, start_pos, num_chars); insert(new_str, into_me, start_pos); length(of_me); copy(from_me, start_pos, num_chars); upcase(str); These are all explained well enough in the book. Fall 2001 (c)opyright Brent M. Dingle 2001
(c)opyright Brent M. Dingle 2001 Challenge Create a program that will read text from a file and replace all the occurrences of the words ‘he’ and ‘she’ with the word ‘it’. The output should be a NEW text file with the changes made. Fall 2001 (c)opyright Brent M. Dingle 2001
Suggested Probs (no grade) page 364 19 Programming pages 369 – 372 22 – do it first without arrays, then with them, then up the number of integers to 50 (instead of just 10). 23 – see if you can use an enumerated data type to index into your 5 element array. 26, 29, 31 Fall 2001 (c)opyright Brent M. Dingle 2001
(c)opyright Brent M. Dingle 2001 End Strings Chapter 9.3 And thus ends Chapter 9 Fall 2001 (c)opyright Brent M. Dingle 2001