필터 지우기
필터 지우기

Recursion Output Problem

조회 수: 1 (최근 30일)
KKR
KKR 2012년 2월 17일
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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by