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)
{
}

}
else if(t2.Length() == 0)
{
if(len > 0)
{

t1.Remove(pos, ch);
Swap_Substring(t1, pos, len -1, t2);
}
}
}``````

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.

My first qs. is what does Add and Remove function do.
2nd Qs. : Why you are using recursion specifically.

======================================================

Try for:

t1 = runner
pos = 2
len = 2
t2 = zeennaabbcc

My first qs. is what does Add and Remove function do.
2nd Qs. : Why you are using recursion specifically.

The add function adds characters to a word and the remove function removes characters.

For example,

t1 = runner

t1.Add(the position of where you want to add the character, this holds the character that is intended to be added). So, if we say

ch = 'h'

Remove stores the character that was removed in ch. For example, where t1 is still runner.

t1.Remove(3, ch); would be removing the letter n. and the word is now runer and ch is now 'n'. It is usually desirable to use a loop.

I am using recursion because I am required to do so for this specific assignment. I know how to swap a sub string regularly, but....they want it a certain way. Thank you for putting up with this awkward framework....

I haven't given it much thought for different cases , but it should work.

``````if(t2.Length() > 0)
{

if(len > 0)
{
t1.Remove(pos, ch);
t2.Remove(0, ch2);
Swap_Substring(t1,pos, len-1, t2);
}

if(len==0)
{
t2.Remove(0,ch);
Swap_Substring(t1,pos, len, t2);
}

}

else if(t2.Length() == 0)
{
if(len > 0)
{
t1.Remove(pos, ch);
Swap_Substring(t1, pos, len -1, t2);
}
}``````

I really really appreciate you taking a look at this for me. It seemed like I just got carried away with if statements for my own good. The code DID work. I just did not know you could call the method more than once inside the procedure. So you can call it as many times as needed and still have your base case there at the bottom???And when you call it the instance where (len == 0) it just bypasses the case where len > 0 correct?

I also did your test case and I saw that things were correct on my up until the 5 character and then from there things went haywire. I see that my program was effectively executing the two if statements in such a fashion, where the len might not be 1 after the first call so things might not get added in continually. Is this somewhat close?

Thank you again for the big help. 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
)
{
//...
}``````

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)

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.

I was thinking,IF you have anything like a substring function or method available for your strings, OR you have access to #include <string> and it works well with this framework, it would be a VERY simple matter to just do this (requires no recursion!):

``````string left, middle, right, result;
//do the work
left=t1.substr(0, pos);
middle=t2;
right=t1.substr(pos + t2.size(), t1.size() - (pos + t2.size()));
//accumulate result
result=left;
result+=middle;
result+=right;``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.89023465,"math_prob":0.86883044,"size":1784,"snap":"2019-26-2019-30","text_gpt3_token_len":550,"char_repetition_ratio":0.09382022,"word_repetition_ratio":0.049132947,"special_character_ratio":0.33183858,"punctuation_ratio":0.14882506,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96446127,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-07-18T09:34:29Z\",\"WARC-Record-ID\":\"<urn:uuid:6879f78d-cd18-4f65-9b5f-ddd8629a0b01>\",\"Content-Length\":\"62504\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:eb699336-e902-4e89-aa69-8c0ac9a82815>\",\"WARC-Concurrent-To\":\"<urn:uuid:c86b4fdb-0624-41fe-9b9d-668743adc73a>\",\"WARC-IP-Address\":\"198.23.117.137\",\"WARC-Target-URI\":\"https://www.daniweb.com/programming/software-development/threads/413264/i-need-some-help-dealing-with-c-and-recursion\",\"WARC-Payload-Digest\":\"sha1:RQXTAFSAFSGWWM4GC5JLTHBMRGKMHD6B\",\"WARC-Block-Digest\":\"sha1:E4JIZ6GZRXHNUSBROBW5ODUDU6UCESYE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-30/CC-MAIN-2019-30_segments_1563195525587.2_warc_CC-MAIN-20190718083839-20190718105839-00006.warc.gz\"}"}