Which version? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Which version?

Actor21Actor21 Posts: 35Member
I've written three versions of a function which returns the lower case of an upper case alpha char passed to it. If it is passed a char which is not an upper case alpha it returns the input unmodified. All three versions have been tested.

Which version do you prefer? Why?

[code]
function locase(ch : char) : char ;
{
case version - jumps to the correct answer and returns
}
begin
case ch of
'A' : locase := 'a' ;
'B' : locase := 'b' ;
'C' : locase := 'c' ;
'D' : locase := 'd' ;
'E' : locase := 'e' ;
'F' : locase := 'f' ;
'G' : locase := 'g' ;
'H' : locase := 'h' ;
'I' : locase := 'i' ;
'J' : locase := 'j' ;
'K' : locase := 'k' ;
'L' : locase := 'l' ;
'M' : locase := 'm' ;
'N' : locase := 'n' ;
'O' : locase := 'o' ;
'P' : locase := 'p' ;
'Q' : locase := 'q' ;
'R' : locase := 'r' ;
'S' : locase := 's' ;
'T' : locase := 't' ;
'U' : locase := 'u' ;
'V' : locase := 'v' ;
'W' : locase := 'w' ;
'X' : locase := 'x' ;
'Y' : locase := 'y' ;
'Z' : locase := 'z'

else
locase := ch
end { case }
end ;
[/code]

[code] function locase(ch : char) : char ;
{
offset version - computes the offset and shifts the char
}
CONST
OFFSET = ord('a') - ord('A') ;
UPPERS = ['A' .. 'Z'] ;

begin
if ch in UPPERS then
locase := chr(ord(ch) + OFFSET)
else
locase := ch
end ;
[/code]

[code] function locase(ch : char) : char ;
{
search version - searches for the correct char
}
var
c : char ;

begin
for c := 'a' to 'z' do
if upcase(c) = ch then begin
locase := c ;
exit
end ;
{
if we get to here then ch was not an upper case letter
so do nothing
}
locase := ch
end ;
[/code]

Comments

  • quikcarlxquikcarlx Hollywood, FlPosts: 26Member
    All 3 ways seem to be okay; some just taking up more memory than others. Why not use the [color=Green][b]lowercase[/b][/color] function that is in the [color=Purple][b]System[/b][/color] unit of Free Pascal 2.6.0?
  • Actor21Actor21 Posts: 35Member
    : All 3 ways seem to be okay; some just taking up more memory than
    : others. Why not use the [color=Green][b]lowercase[/b][/color]
    : function that is in the [color=Purple][b]System[/b][/color] unit of
    : Free Pascal 2.6.0?
    :
    I'm not using Free Pascal. I'm using Turbo Pascal 7.0
Sign In or Register to comment.