Strings

[Table of Contents]

String is not a predefined data type in Kyan Pascal 2.x. If you declare a String type as an array of characters (String = Array[1..Maxstring] of Char;), you can use the following string subroutines which have been predefined in Kyan Pascal include files:


To use a string manipulation routine, declare the maximum size of the string as a Constant named Maxstring, declare a user defined String data type, and include the string routine include file.

There are also a few predefined string conversion subroutines (procedures and functions) available as well:


To use these string conversion routines, you will need to look at the include files to see what length strings these routines are expecting and returning.

Because of how strings work in Kyan Pascal, there are two things to note.

First, if you create a string Name as an array of 20 characters, when you assign a value to Name, the value assigned must also be exactly 20 characters, though trailing characters can be spaces. Otherwise you will get a compile time error.

Secondly, if you print the string Name to the screen, all twenty characters, including the spaces, will be displayed. This is usually not what you want. You will have to use formatting to adjust the output to achieve the desired result.

Here is an example program (STRINGS.ATR Disk Image) showing assignment and printing with sample output:

(* STRINGS.P *)
(* Kyan Pascal 2.x / Atari 8-bit *)

program strings(input,output);
const
   Maxstring = 20;
type
   String = Array[1..Maxstring] of Char;
Var
   Name : String;

#i length.i

(* Main Program *)
begin
   write(chr(125)); (* Clear screen *)
   
   (* This assignment will generate *)
   (* a compile time error          *)
   (* Name := 'Blaise Pascal';      *)

   (* This assignment will work     *)
   Name := 'Blaise Pascal       ';

   (* As is *) 
   writeln('Hello, ',Name,'. Welcome.');
   (* With formatting *)
   writeln('Hello, ',Name:Length(Name),'. Welcome.');

end.


No comments:

Post a Comment