[comp.lang.pascal] Program

11SSTEIN%GALLUA.BITNET@CUNYVM.CUNY.EDU (01/22/88)

Hello all PASCAL lovers....

I am wondering if there is a way to do this:

BASIC PROGRAM

 10 A$="ABCDEFGH"
 11 FOR I=1 TO LEN(A$)
 12 PRINT MID$(A$,I,1);
 13 NEXT I

 RUN

 A
 AB
 ABC
 ABCD
 ABCDE
 ABCDEF
 ABCDEFG
 ABCDEFGH

 How can you do this in PASCAL? How do I tell the program that I want the
 5th character of a string?

- Scott

mjv1@sphinx.uchicago.edu (Michael Vinson) (01/22/88)

In article <11401@brl-adm.ARPA> 11SSTEIN%GALLUA.BITNET@CUNYVM.CUNY.EDU writes:
>
>I am wondering if there is a way to do this:
>
>BASIC PROGRAM
> 10 A$="ABCDEFGH"
> 11 FOR I=1 TO LEN(A$)
> 12 PRINT MID$(A$,I,1);
> 13 NEXT I
>
> A
> AB
> ABC
> ...
> How can you do this in PASCAL? How do I tell the program that I want the
> 5th character of a string?
>
>- Scott

There are several ways. For example in Turbo Pascal, as in many other
Pascals, there is the COPY function defined as

function copy(st : string; pos,num : integer) : string;

which returns the substring of st from position pos to position pos+num.
Thus the fifth character of the string str is copy(str,5,1).
Alternatively, in most Pascals the string type is equivalent to an
array of char, so that you can access entries directly. E.g. if
str is set to 'abcdef' then str[2] is 'b'. (The first method is the
recommended.)

Michael Vinson