0

I have a string pointer like below,

char *str = "This is cool stuff";

Now, I've references to this string pointer like below,

char* start = str + 1;
char* end = str + 6;

So, start and end are pointing to different locations of *str. How can I copy the string chars falls between start and end into a new string pointer. Any existing C++/C function is preferable.

1
  • this is 'c', not 'c++', in 'c++' one would use either a string class or an array class (I HOPE). Commented Nov 5, 2009 at 13:18

7 Answers 7

3

Just create a new buffer called dest and use strncpy

char dest[end-start+1];
strncpy(dest,start,end-start);
dest[end-start] = '\0'
Sign up to request clarification or add additional context in comments.

3 Comments

previously it was "dest[256]"
Keep in mind that if there is a \0 character somewhere between start and end, the copying will stop there.
@ndim - we've already established that start and end are pointers within a C string, so therefore there can't be a 0 byte anywhere except at the end.
2

Use STL std::string:


#include 
const char *str = "This is cool stuff";
std::string part( str + 1, str + 6 );

This uses iterator range constructor, so the part of the C-string does not have to be zero-terminated.

Comments

0

It's best to do this with strcpy(), and terminate the result yourself. The standard strncpy() function has very strange semantics.

If you really want a "new string pointer", and be a bit safe with regard to lengths and static buffers, you need to dynamically allocate the new string:

char * ranged_copy(const char *start, const char *end)
{
  char *s;

  s = malloc(end - start + 1);
  memcpy(s, start, end - start);
  s[end - start] = 0;

  return s;
}

Comments

0

If you want to do this with C++ STL:

#include <string>
...
std::string cppStr (str, 1, 6); // copy substring range from 1st to 6th character of *str
const char *newStr = cppStr.c_str(); // make new char* from substring

Comments

0
char newChar[] = new char[end-start+1]]
p = newChar;
while (start < end)
  *p++ = *start++;

8 Comments

You have a reserved word for your string name and no delete[].
A bit complicated compared to Arkaitz Jimenez's solution. Also, isn't 'new' a protected keyword that cannot be used as variable name? Doesn't copmpile on VC9.
@Sid, if you delete it, then you don't have the string any more.
@Paul, I think @Sid he means when you've finished with the string you need to delete[] it Also, there's no check for end > start, so you could get issues if that's the case. In this simple example end is > start, but it's a good check to have on more generic code.
@pxb, you always have to delete the memory once you're done with it. The question wasn't about the full life cycle of the variable, just how to create it.
|
0

This is one of the rare cases when function strncpy can be used. Just calculate the number of characters you need to copy and specify that exact amount in the strncpy. Remember that strncpy will not zero-terminate the result in this case, so you'll have to do it yourself (which, BTW, means that it makes more sense to use memcpy instead of the virtually useless strncpy).

And please, do yourself a favor, start using const char * pointers with string literals.

Comments

0

Assuming that end follows the idiomatic semantics of pointing just past the last item you want copied (STL semantics are a useful idiom even if we're dealing with straight C) and that your destination buffer is known to have enough space:

memcpy( buf, start, end-start);
buf[end-start] = '\0';

I'd wrap this in a sub-string function that also took the destination buffer size as a parameter so it could perform a check and truncate the result or return an error to prevent overruns.

I'd avoid using strncpy() because too many programmers forget about the fact that it might not terminate the destination string, so the second line might be mistakenly dropped at some point by someone believing it unnecessary. That's less likely if memcpy() were used. (In general, just say no to using strncpy())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.