Hey everyone, I am pretty new to writing procedures that deal with this idea of recursion. I am trying to get into that certain frame of mind but I am having a little bit of trouble at the last minute. My goal was to implement a text operation called swap substring using recursion.
This is what I have so far:
procedure_body Swap_Substring (
alters Text& t1,
preserves Integer pos,
preserves Integer len,
alters Text& t2
)
{
//first impelementation of recursion of swap_sub_string, beginning
//starting at t2.
object Character ch, ch2;
if(t2.Length() >0)
{
debug("t1 = " << t1 << "t2 = " << t2 << "pos = " << pos << "len=" << len << '\n');
if(len > 0)
{
t1.Remove(pos, ch);
t2.Remove(0, ch2);
Swap_Substring(t1,pos, len-1, t2);
}
if(len > 0)
{
t2.Add(0, ch);
t1.Add(pos, ch2);
}
}
else if(t2.Length() == 0)
{
if(len > 0)
{
t1.Remove(pos, ch);
Swap_Substring(t1, pos, len -1, t2);
t2.Add(0, ch);
}
}
}
I'm sure some have noticed this is not straight C++. This is effectively C++ with a designated framework I have to use for university work. Any correct C++ would probably compile though. The object of this program is to swap a sub string, like so:
t1 = smile
pos = 1(where to start)
len = 2(letters to take out)
t2 = hey(to be swapped in)
so it should be
t1 = sheyle
pos = 1
len = 2
t2 = mi
My code is ALMOST close to working completely, it looks like I'm just off by a letter.
SAMPLE OF MY INPUT AND OUTPUT:
t1 = runner
pos = 2
len = 2
t2 = zee
correct output:
t1 = ruzeeer
pos = 2
len = 2
t2 = nn
my output:
t1 = ruzeer
pos = 2
len = 2
t2 = nne
See what I mean. It's like it's one letter away. This is probably some silly error, but for the life of me I can't find it after searching hours. I would GREATLY appreciate it if anyone could help me.
Thank you and have a good evening.