Jump to content
  • Advertisement
Sign in to follow this  
Blednik

Best way to remove a substring from a C string?

This topic is 1492 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'm poking around with the C string functions. Don't ask, I'd personally just use C++ for the task, but it has to be pure C in this case. Anyway here's the problem. I have a null-terminated string, and I have to remove a certain substring from it that begins on location a, and ends on location b. I'm doing this on windows in MinGW. Consider this sample:

char * mystr = "sometestingtennisbabesjunk";const int a = 4;const int b = 11;removesubstring(mystr, a, b);
The result would be: "sometennisbabesjunk", characters that were removed from location 4 to 11: "testing".

What is the best way in C to do this? Is there a dedicated C string function for this task? I couldn't find one.

Share this post


Link to post
Share on other sites
Advertisement

Also if you implement it manually make sure you validate your bounds, that is:

if ((b < a) || (b > strlen(str))))
    /* abort! */

Otherwise you're just setting yourself up for scribbling all over your stack or heap.

Share this post


Link to post
Share on other sites

Also if you implement it manually make sure you validate your bounds, that is:

if ((b &amp;amp;lt; a) || (b &amp;amp;gt; strlen(str))))    /* abort! */
Otherwise you're just setting yourself up for scribbling all over your stack or heap.
Shouldn't the sanity checks be like:
len = strlen(str);
if ( (!len) || (b <= a) || (a >= len) ) {... abort}
if (b > len) b = len;
EDIT: Stupid HTML

Share this post


Link to post
Share on other sites

 

Also if you implement it manually make sure you validate your bounds, that is:

if ((b &amp;amp;lt; a) || (b &amp;amp;gt; strlen(str))))    /* abort! */
Otherwise you're just setting yourself up for scribbling all over your stack or heap.
Shouldn't the sanity checks be like:
len = strlen(str);
if ( (!len) || (b <= a) || (a >= len) ) {... abort}
if (b > len) b = len;
EDIT: Stupid HTML

 

 

Depends how you want to use your function. I personally prefer to not allow nonsensical input at all, thus if b is beyond the string, I would reject it. If you prefer to clamp it to the string's length instead like some string functions do, that's fine too, as long as you document that behaviour. If you don't want to allow a == b, that's fine as well. And of course you should reject null char* pointers, forgot that one (though it is obvious). Notice that !((b < a) || (b > len)) implies len > 0 (or a = b = 0) since a and b are unsigned.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!