Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

Which version?

$
0
0
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?

   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 ;


   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 ;


   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 ;


Viewing all articles
Browse latest Browse all 2703

Trending Articles