|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
6-7 String Functions and ProceduresWhen working with text editors, you sometimes need to cut and paste, delete a part from here, and insert a part there. The tools that make these operations possible are included in the modern implementations of Pascal to help the programmer process strings. Some of them are called functions because they return a value which replaces the function call (e.g., LENGTH). Others are called procedures, as they perform specific operations that do not necessarily return a value (e.g., WRITELN). They are all shown in Table 6-1. In addition to these tools you may find more functions, procedures, or operators in a specific implementation, but here we are concerned only with the most common tools, which are almost standardized.
CONCATAs an example of using the function CONCAT, you can concatenate the three strings John , M. , and Smith and assign the result to a string variable Name, as follows: Name:= CONCAT('John ','M. ','Smith'); Now the variable Name contains the complete name: John M. Smith. In Turbo Pascal the operator + may also be used to concatenate strings: Name:= 'John '+ 'M. '+ 'Smith'; The variable Name has the same contents as before. COPYUsing the function COPY you can do the opposite, i.e., extract a substring from the string Name. The following statement extracts the first name from the string Name and assigns it to the variable FirstName: FirstName:= COPY(Name, 1, 4); As you can see, you have to include the starting position of the extracted substring (1 in this case) and the length of the substring (4 in this case). POSThe function POS returns an integer that indicates the position of the first occurrence of a substring in a string. For example, the statements: Str1:= 'This is a test'; WRITELN(POS('is', Str1)); result in displaying the number 3, which is the position of the letter i in This. DELETETo delete the substring Smith from a name string, use the DELETE procedure as follows: DELETE(Name, 9, 5); Note that the substring Smith starts at the 9th position and contains 5 characters. Using a procedure changes the value of the original variable Name, while using a function does not. If you checked the contents of the variable now, it would be John M. . INSERTDont worry, because you can insert the substring in the right place again. Use the INSERT procedure to put the last name Smith back in the string: INSERT('Smith', Name, 9) Now the variable Name contains John M. Smith. The following program demonstrates the use of string functions. It accepts from you the first, middle, and last name and produces the complete name, including the trailing spaces. Also, the middle name is converted to an initial. { -------------------- figure 6-11 -------------------- } PROGRAM StringFunctions1(INPUT,OUTPUT); VAR Name :STRING[30]; First, Middle, Last :STRING[10]; BEGIN WRITE('Please enter your first name: '); READLN(First); First:= CONCAT(First, ' '); WRITE('Please enter your middle name: '); READLN(Middle); Middle:= COPY(Middle, 1, 1); Middle:= CONCAT(Middle, '. '); WRITE('Please enter your last name: '); READLN(Last); Name := CONCAT(First, Middle, Last); WRITELN; WRITELN('Your complete name is: ',Name) END. Sample run: Please enter your first name: Sally Please enter your middle name: Ann Please enter your last name: Abolrous Your complete name is: Sally A. Abolrous
SummaryIn this chapter you learned how the input statements READ and READLN work with numeric values and characters. You also learned how to use the end-of-line function EOLN to read a line of text from the keyboard, and the end-of-file function EOF to read a file of text. You also learned some of the important string-processing functions and procedures which are available in the modern implementations of Pascal:
Most importantly, through the examples and drills you gained experience in text processing with both strings and individual characters.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |