Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Drill 6-4

Write a program to scramble three strings. The following is an example of the output for the strings “WHO,” “ARE,” and “YOU”:

WHO ARE YOU
WHO YOU ARE
ARE WHO YOU
ARE YOU WHO
YOU WHO ARE
YOU ARE WHO

6-7 String Functions and Procedures

When 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.

Table 6-1 String functions and procedures

Form Use

Functions:
LENGTH(str) Returns the number of characters in the string “str.”
CONCAT(str1, str2,...) Returns the string formed by concatenating “str1,” “str2,”...
COPY(str, pos, len) Returns a substring from the string “str,” starting at the position “pos,” with length “len.”
POS(str1, str2) Returns the position of the first occurrence of the first character of “str1” within “str2.”
If “str1” does not occur within “str2” it returns zero.
Procedures:
INSERT(str1, str2, pos) Inserts the string “str1” into the string “str2,” at the position “pos.”
DELETE(str, pos, len) Deletes a substring from a string “str” starting from position “pos” with length “len.”

CONCAT

As 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.

COPY

Using 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).

POS

The 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.”

DELETE

To 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. ’.

INSERT

Don’t 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

Drill 6-5

Modify the last program to make it capitalize the first letter of each name if lowercase.

Summary

In 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:

  CONCAT
  COPY
  POS
  INSERT
  DELETE

Most importantly, through the examples and drills you gained experience in text processing with both strings and individual characters.


Previous Table of Contents Next


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.