function s2 = caesar ( s1, k )

%*****************************************************************************80
%
%% caesar() applies a Caesar shift cipher to a string.
%
%  Discussion:
%
%    The Caesar shift cipher incremented each letter by 1, with Z going to A.
%
%    This function can apply a Caesar shift cipher to a string of characters,
%    using an arbitrary shift K, which can be positive, negative or zero.
%
%    Letters A through Z will be shifted by K, mod 26.
%    Letters a through z will be shifted by K, mod 26.
%    Non-alphabetic characters are unchanged.
%
%    s2 = s_to_caesar ( s1, 1 ) will apply the traditional Caesar shift cipher.
%    s3 = s_to_caesar ( s2, -1 ) will decipher the result.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    31 January 2016
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string S1, a string of characters.
%
%    integer K, the increment.
%
%  Output:
%
%    string S2, the string of enciphered characters.
%
  iacap = uint8 ( 'A' );
  ialow = uint8 ( 'a' );

  s2 = s1;
  for i = 1 : length ( s1 )
    ui1 = uint8 ( s1(i) );
    i1 = double ( ui1 );
    if ( iacap <= i1 && i1 <= iacap + 25 )
      i2 = mod ( i1 + k - iacap, 26 ) + iacap;
      s2(i) = char ( i2 );
    elseif ( ialow <= i1 && i1 <= ialow + 25 )
      i2 = mod ( i1 + k - ialow, 26 ) + ialow;
      s2(i) = char ( i2 );
    end
  end
  
  return
end
