Clear Filters
Clear Filters

Recursion Output Problem

1 view (last 30 days)
KKR
KKR on 17 Feb 2012
Hi All,
I am writing a recursion problem to do some string manipulation. Seems like I am almost there. However, my function operates perfectly and does perform the manipulation I want. But, during exit condition, it performs one more recursion, and somehow it screws up the output, at the very end. It is as following: I would sincerely appreciate your help/inputs.
function strnew = stringop1rec(strogi, str1, str2,rtflag, ltflag)
lnstr = lenfunc(strogi); lnstr1 = lenfunc(str1); lnstr2 = lenfunc(str2); dff = lnstr1 - lnstr2;
if rem(lnstr,2)~=0
disp('The String is Length is Odd Numbers. The Function ignored the Middle Character.')
end
%Depending upon number of arguments passed into Function Starting Position
%are initialized
if nargin == 3
rtflag=1; ltflag=lnstr;
elseif nargin == 4
disp('Please enter the starting position for Other half Character C2 :')
else
rtflag = rtflag+1; ltflag = ltflag-1;
end
ii=0; jj=0;
%Calculates the occurence of Character C1 in first half of string
for i=rtflag:floor(lnstr/2)
if strogi(i:i+lnstr1-1) == str1;
ii=i;
break;
end
end
%Calculates the occurence of Character C2 in second half of string
for j=ltflag:-1:ceil(lnstr/2+1)
if strogi(j-lnstr2+1:j) == str2;
jj = j;
break;
end
end
s1 = 0; s2=0;
strnew=strogi;
%The Following Loops are written to avoid using Concatenante Function.
if ii~=0 && jj~=0
for rti=rtflag:floor(lnstr/2)
if rti< ii
strnew(rti)=strogi(rti);
elseif rti>=ii && rti<ii+lnstr2
s2 = s2+1;
strnew(rti)=str2(s2);
else
strnew(rti)=strogi(rti+dff);
end
end
for lti=ltflag:-1:floor(lnstr/2)
if lti> jj
strnew(lti)=strogi(lti);
elseif lti<=jj && lti>jj-lnstr1
s1 = s1+1;
strnew(lti)=str1(lnstr1-s1+1);
else
strnew(lti)=strogi(lti+dff);
end
end
stringop1rec(strnew,str1,str2,ii,jj); %If Code finds a pair of substrings, performs Recursion
end

Answers (0)

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!