Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Which version? Posted by Actor21 on 24 Sept 2012 at 12:41 AM
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 ;

Report
Re: Which version? Posted by quikcarlx on 25 Sept 2012 at 9:34 PM
All 3 ways seem to be okay; some just taking up more memory than others. Why not use the lowercase function that is in the System unit of Free Pascal 2.6.0?
Report
Re: Which version? Posted by Actor21 on 26 Sept 2012 at 2:30 PM
: All 3 ways seem to be okay; some just taking up more memory than
: others. Why not use the lowercase
: function that is in the System unit of
: Free Pascal 2.6.0?
:
I'm not using Free Pascal. I'm using Turbo Pascal 7.0



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.