Jump to content
  • Advertisement
Sign in to follow this  
CoB_ChrizZz

c-string fuction: insert

This topic is 2507 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i got this excercise:

Implement an elementary function insert(), that inserts the part-string "s" from the index "start".
If the index is too big or to small, nothing will happen.
of course the manipulation demands a resizing of the String-Object

void String::insert(int start, const String& s)
{
//my attempt:
http://pastebin.com/7KUshGF3
}

String is a selfmade class whose dynamicly allcoated variable i shall manipulate with this function is:
char *str;

It took me quite a while to implement this and i really doupt i will write this on paper (in time) in case it is part of my test on friday.
I thought there must be a smarter way to do this.
So i came here to ask for help once more ^^

Share this post


Link to post
Share on other sites
Advertisement
Here's a few things I picked up on:

- You don't allow insertion at the end (i.e. when start == str.size()).
- You don't need to call new twice. Once is enough. Fixing that should also fix the memory leak.
- You don't need the loops. Two or three calls to memcpy() will work just as well.

Share this post


Link to post
Share on other sites
thank you Adam_42 !
But i may only use the helper functions: strcpy(), strcat() and strlen()
thanks about pointing out a memory leak (i will look after that later) and the issue at the end ! :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!