function D = censor(str, A)
% Replace all occurrences of string str in character matrix A, regardless
% of case, with 'X's.
% A is a matrix of characters.
% str is a string.  Assume that str is never split across two lines in A.
% D is A with 'X's replacing the censored string str.
%
% Example:   A = ['Use MATLAB  '; ...
%                 'in that lab.']
%
%   and      str = 'lab'
%
%   Then     D = ['Use MATXXX  '; ...
%                 'in that XXX.']

D= A;
ns= length(str);
[nr,nc]= size(A);

% Build a string of 'X's of the correct length
Xs= char(zeros(1,ns));  % Initialization not necessary, but good practice  
                        % to initialize to correct size and TYPE
for k= 1:ns
    Xs(k)= 'X';
end

% Traverse the matrix to censor string str
for r= 1:nr
    for c= 1:nc-ns+1
        if  strcmpi(str,A(r,c:c+ns-1))
            D(r,c:c+ns-1)= Xs;  % note that this is vectorized code
        end
    end
end
