A string is a one-dimensional array of ASCII characters, that is, an array of type char. Using various built-in
procedures and functions, you can manipulate strings in the following ways: add characters to them, delete characters
from them, and combine and compare them.
To specify a string type, after the reserved word string, tell the compiler the maximum number of characters a
variable of that type will contain. For example:
type bigstring
= string[80];
var S:
bigstring;
This declares S as a string variable that can consist of (at most) 80 characters. If you attempt to assign more
than n characters to a variable of type string[n], the excess characters are lost.
The following functions and procedures manipulate string variables:
function Concat(S1,
S2 {, S3,...,Sn})
-returns a string composed of <S1> through <Sn> joined together ("concatenated");
the plus sign can also be used to perform this operation.
function Copy(S,
Position, Len)
-returns a string composed of <Len> characters in string <S> starting with character # <Position>.
procedure Delete(S,
Position, Num)
-deletes <Num> characters from string <S> starting with character # <Position>.
procedure Insert(Source,
Destination, Position)
-inserts <Source> into <Destination> starting at character # <Position>.
function Length(S)
-returns current length of string S.
function Pos(Pattern,
Target)
-returns the position of <Pattern> within <Target>.
Comparing Strings
Strings are compared based on their underlying ASCII values and length, in that order. For example, "cat"
is less than "dog" because the ASCII value of 'c' is less than the ASCII value of 'd'. Two strings are
equal only if they contain exactly the same characters. If two strings have different lengths, but are identical
up to the length of the shorter string, then the shorter string is considered to be less than the longer string.
This means that "cat" is less than "cats".
Case is significant when comparing strings. "Cat" is less than "cat" because uppercase letters
have lower ASCII values than their lowercase equivalents.
program StringTest;
type MaxString
= string;
var InString:
MaxString;
procedure
ToLower (var
S: MaxString);
{ convert uppercase letters in S to lowercase
equivalent }
var Index
: integer;
begin
for Index
:= 1 to
Length(S) do
if S[Index]
in ['A'..'Z']
then S[Index]
:= Chr(Ord(S[Index]) + 32);
end; { ToLower }
procedure
CountLetters(S: string);
var
C: char;
I: integer;
LetterCount : array['a'..'z'] of integer;
begin
for C :=
'a' to
'z' do LetterCount[C]
:= 0;
for
I := 1 to
length(S) do lettercount[s[I]]
:= lettercount[s[I]] + 1;
Writeln('This string contains: ');
for
C := 'a' to
'z' do
begin
Write(LetterCount[c]:3,' ',c,' ');
if (ord(C)
mod 6) = 0 then
Writeln; { six letters per line }
end;
Writeln;
end; { CountLetters }
var I
: integer;
begin
{ StringTest }
repeat
Write('Enter a string: ');
Readln(inString);
ToLower(inString);
CountLetters(inString);
Write('Backwards: ');
for
I := Length(inString) downto
1 do Write(InString[I]);
Writeln;
until InString
= 'quit';
end. { StringTest }