|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
Standard Functions for CharactersThere are four standard functions that are dedicated to handle character operations:
You can get the ordinal number of any character by using the function ORD, as in the following example: WRITELN(ORD('A')); This statement displays the ordinal of the character A, which is 65. In the following program the user enters a character and the program displays the corresponding ordinal number. { ------------------------------ figure 2-4 ------------------------------ } { Displaying the Ordinal Number of a Character } PROGRAM OrdinalNumber(INPUT,OUTPUT); VAR SingleChar:CHAR; BEGIN WRITE('Give me a character, please: '); READLN(SingleChar); WRITELN('The ordinal number of this character is ', ORD(SingleChar)); READLN { The program will pause until you press Enter } END. A sample run of the program gives the following: Give me a character, please: A ----> Type "A" and press Enter The ordinal number of this character is 65 ----> The program output
The counterpart of ORD is the function CHR, which takes an ordinal number as a parameter and returns the character that corresponds to this number. Look at this example: WRITELN(CHR(66)); This statement displays the letter "B." In the following program, the user enters an ordinal number and the program displays the corresponding character. { ------------------------------ figure 2-5 ------------------------------ } { Displaying the Character, Knowing its Ordinal Number } PROGRAM CharDisplay(INPUT,OUTPUT); VAR OrdinalNum:BYTE; BEGIN WRITE('Give me a number between 0 and 255: '); READLN(OrdinalNum); WRITELN('This corresponds to the character "', CHR(OrdinalNum),'"'); WRITELN('Press ENTER to continue...'); READLN { The program will pause until you press Enter } END. A sample run of the program gives the following: Give me a number between 0 and 255: 66 ----> Enter the number "66" This corresponds to the character B" ----> The program output Press ENTER to continue...
The following program demonstrates the use of the functions PRED and SUCC. You enter a character and the program displays the previous and the next characters. { ------------------------------ figure 2-6 ------------------------------ } { The Predecessor and the Successor to a Character } PROGRAM CharPredAndSucc(INPUT,OUTPUT); VAR Letter: CHAR; BEGIN WRITE('Please Enter a character: '); READLN(Letter); WRITELN('The Predecessor to this character is "',PRED(Letter),'"'); WRITELN('The Successor to this character is "',SUCC(Letter),'"'); WRITELN('Press ENTER to continue...'); READLN END. A sample run gives the following: Please Enter a character:K ----> Enter the character "K" The Predecessor to this character is "J" ----> The program response The Successor to this character is "L" Press ENTER to continue... You can use numbers or any special symbols from your keyboard to test this program. Remember, though, that some machines (mainframes) use a different sequence known as the EBCDIC code. You may also use the function ORD with the type INTEGER, in which case it returns the sequence of the integer in the set of integers (from -(MAXINT+1) to MAXINT). Thus:
The functions SUCC and PRED work with integers in the same way, which means:
Some programmers increment their counters with a statement like this: Counter:= SUCC(Counter); If you replace the type CHAR by the type INTEGER in the last program (figure 2-6), you can test these relations. Strings in Standard PascalAs mentioned earlier, you can represent a string constant using single quotes like this: 'This is a string enclosed in single quotes' To include an apostrophe in the string constant, you need two of them: 'This is an apostrophe '' included in a string' You can also assign a string to a named constant: CONST Name = 'Sally Shuttleworth'; After this declaration you can use the named constant Name instead of the string itself, but remember that in the program you cannot assign any value to a named constant. The string variable, however, is not defined in standard Pascal. A string, in standard Pascal, is stored in a PACKED ARRAY OF CHAR which is declared like this: VAR Name: PACKED ARRAY[1..15] OF CHAR; This declaration lets you store a string of exactly 15 characters in the variable Nameno more, no less. Look at the following example, where the variable Message is declared and assigned the string 'Press any key... '. Extra spaces are padded to the end of the string constant to make it fit into the variable message, which was declared as a PACKED ARRAY OF CHAR 21 characters long. { ------------------------------ figure 2-7 ------------------------------ } { Packed Array Of Characters } PROGRAM PackedArray(OUTPUT); VAR Message:PACKED ARRAY[1..21] OF CHAR; BEGIN Message:= 'Press any key... '; WRITELN(Message) END. The output is: Press any key...
|
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. |