function filename_new = filename_ext_swap ( filename, ext )

%*****************************************************************************80
%
%% filename_ext_swap() replaces the current "extension" of a file name.
%
%  Discussion:
%
%    The "extension" of a filename is the string of characters
%    that appears after the LAST period in the name.  A file
%    with no period, or with a period as the last character
%    in the name, has a "null" extension.
%
%  Example:
%
%          Input           Output
%    ================     =============
%    FILENAME     EXT     FILENAME_NEW
%    
%    bob.for      obj     bob.obj
%    bob.bob.bob  txt     bob.bob.txt
%    bob          yak     bob.yak
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    15 August 2005
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string FILENAME, a file name.
%    On output, the extension of the file has been changed.
%
%    string EXT, the extension to be used on the output
%    copy of FILE_NAME, replacing the current extension if any.
%
%  Output:
%
%    string FILENAME_NEW, a copy of the input file name,
%    with the new extension.
%
  filename_len = length ( filename );

  ext_len = length ( ext );

  period = filename_len + 1;

  for i = filename_len : -1 : 1
    if ( filename(i:i) == '.' )
      period = i;
      break
    end
  end

  filename_new(1:period-1) = filename(1:period-1);
  filename_new(period) = '.';
  filename_new(period+1:period+ext_len) = ext(1:ext_len);

  return
end
