Search Program on this blog

Thursday 20 August 2015

Write a function called replace that takes three inputs: a cell vector (row or column) of strings and two characters: c1 and c2. The function returns the cell vector unchanged except that each instance of c1 in each string is replaced by c2. You are not allowed to use the built-in function strrep.

function c = replace(A,c1,c2)
%Input A is a cell vector of strings, c1, c2: two characters
%Output c is a cell vector
%Example: A={ 'XOXO', 'XOXO' }
%         c = replace(A,'X','Y')
[m,n]=size(A); %dimensions of A
for i=1:m
    for j=1:n
        a=length(A{i,j}); %length of each string in A
        for k=1:a
            if A{i,j}(k)==c1
                A{i,j}(k)=c2;
            end
        end
    end
end
c=A;

1 comment: