SRL Archive - Delphi/FPC Help and Tutorials

toupper/tolower


Craig`

2009-06-28 20:59:32

The other thread got deleted as I've added toupper.
Note, this time I do not need to use one method to use another.

To compile this I used Lazarus, it will not work with SCAR as it is pure pascal at the moment.
- To get it to work, just change # := s; to result := s; and write to writeln

program blah;

function toupper(s: string): string;
var
i: integer;
begin
for i := 1 to length(s) do
begin
if s[i] in ['a'..'z'] then
begin
s[i] := chr(ord(s[i]) - 32);
end;
end;
toupper := s;
end;

function tolower(s: string): string;
var
i: integer;
begin
for i := 1 to length(s) do
begin
if s[i] in ['A'..'Z'] then
begin
s[i] := chr(ord(s[i]) + 32);
end;
end;
tolower:= s;
end;

begin
writeln(toupper('red'));
writeln(tolower('RED'));
readln;
end.

Gu3st...

2009-06-29 00:02:33

This is the same as LowerCase and UpperCase :)

Craig`

2009-06-29 00:14:56

Well yeah SCAR comes with these, but pascal doesn't originally :)

Cazax

2009-06-29 00:23:06

Isn't there something like this in StrUtils?

Craig`

2009-06-29 13:43:14

I'm not quite sure with that Cazax, anyways I found it an interesting piece of code :)