File : rot13.sp


$ spar rot13
Ovt swbeqf irk dhvpx jnygm alzcu!


#!/usr/local/bin/spar

pragma annotate( summary, "rot13" );
pragma annotate( description, "Implement a 'rot-13' function (or procedure, class," );
pragma annotate( description, "subroutine, or other 'callable' object as appropriate" );
pragma annotate( description, "to your programming environment).  The definition of the" );
pragma annotate( description, "rot-13 function is to simply replace every letter of the" );
pragma annotate( description, "ASCII alphabet with the letter which is 'rotated' 13" );
pragma annotate( description, "characters 'around' the 26 letter alphabet from its" );
pragma annotate( description, "normal cardinal position (wrapping around from 'z' to" );
pragma annotate( description, "'a' as necessary). Thus the letters 'abc' become 'nop'" );
pragma annotate( description, "and so on. Technically rot-13 is a 'monoalphabetic" );
pragma annotate( description, "substitution cipher' with a trivial 'key'. A proper" );
pragma annotate( description, "implementation should work on upper and lower case" );
pragma annotate( description, "letters, preserve case, and pass all non-alphabetic" );
pragma annotate( description, "characters in the input stream through without" );
pragma annotate( description, "alteration." );
pragma annotate( see_also, "http://rosettacode.org/wiki/Rot-13" );
pragma annotate( author, "Ken O. Burtch" );
pragma license( unrestricted );

pragma restriction( no_external_commands );

procedure rot13 is

  function to_rot13( s : string ) return string is
    ch : character;
    result : string;
  begin
    for i in 1..strings.length( s ) loop
        ch := strings.element( s, i );
        if strings.is_letter( ch ) then
           if (ch in 'A'..'M') or (ch in 'a'..'m' ) then
              ch := strings.val( numerics.pos( ch ) + 13 );
           else
              ch := strings.val( numerics.pos( ch ) - 13 );
           end if;
        end if;
        result := @ & ch;
    end loop;
    return result;
  end to_rot13;

begin
   ? to_rot13( "Big fjords vex quick waltz nymph!" );
end rot13;

-- VIM editor formatting instructions
-- vim: ft=spar