function s2 = boundary_word_transform ( s, udlr )

%*****************************************************************************80
%
%% boundary_word_transform() transforms a boundary word.
%
%  Discussion:
%
%    The boundary word defines a path along the boundary of a polyomino.
%    This path has a sense: +1 for counterclockwise, -1 otherwise.
%    The transformed boundary word will have the same sense as the original.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    20 June 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    char s(*): a string of the characters 'u', 'd', 'l' and 'r' that should
%    represent a path around the boundary of a polyomino.
%
%    char udlr(4): a permutation of the characters.  The following are legal:
%    'udlr': no change
%    'udrl': up/down reflection
%    'dulr': left/right reflection
%    'durl': up/down reflection and left/right reflection = rotation by 180
%    'lrdu': rotation by 90
%    'lrud': rotation by 90, then up/down reflection
%    'rlud': rotation by 270
%    'rldu': rotation by 270, then up/down reflection
%
%  Output:
%
%    string s2: the boundary word for the transformed string.
%
  check = boundary_word_check ( s );

  if ( ~ check )
    s2 = '';
    return
  end

  s = lower ( s );

  s2 = s;
  i = find ( s == 'u' );
  s2(i) = udlr(1);
  i = find ( s == 'd' );
  s2(i) = udlr(2);
  i = find ( s == 'l' );
  s2(i) = udlr(3);
  i = find ( s == 'r' );
  s2(i) = udlr(4);
%
%  If the transformation would reverse the sense of the boundary word,
%  reverse the boundary word to preserve the sense.
%
  if ( is_octave ( ) )

    if ( ( udlr(1) == 'u' && udlr(2) == 'd' && udlr(3) == 'r' && udlr(4) == 'l' ) || ...
         ( udlr(1) == 'u' && udlr(2) == 'd' && udlr(3) == 'r' && udlr(4) == 'l' ) || ...
         ( udlr(1) == 'u' && udlr(2) == 'd' && udlr(3) == 'r' && udlr(4) == 'l' ) || ...
         ( udlr(1) == 'u' && udlr(2) == 'd' && udlr(3) == 'r' && udlr(4) == 'l' ) )
      s2 = s2(end:-1:1);
    end

  else

    if ( string ( udlr ) == 'udrl' | ...
         string ( udlr ) == 'dulr' | ...
         string ( udlr ) == 'lrud' | ...
         string ( udlr ) == 'rldu' )
      s2 = s2(end:-1:1);
    end

  end


  return
end
