Copy end of string in C
Asked Answered
J

4

7

I am trying to use strncpy to only copy part of a string to another string in C.

Such as:

c[] = "How the heck do I do this";

Then copy "do this" to the other string, such that:

d[] = "do this"

Help is appreciated.

Jewelfish answered 20/9, 2012 at 0:27 Comment(0)
A
12

Just pass the address of the first letter you want to copy: strcpy(dest, &c[13]).

If you want to end before the string ends, use strncpy or, better, memcpy (don't forget to 0-terminate the copied string).

Alethaalethea answered 20/9, 2012 at 0:29 Comment(5)
So assuming 'd' in 'do' is in the 20th position strncpy(dest, &c[20]), is that correct? or should "dest" be replaced by array d?Jewelfish
Alomost. strncpy needs a length, strncpy(dest, &c[20], 8). Whether dest is a char* pointing to a sufficiently large malloced memory area or a sufficiently large char[N] doesn't matter.Alethaalethea
Ok that makes sense, strcpy it is, but how do I assign that to array d then?Jewelfish
What do you mean with assigning that to array d? strcpy(d, &c[20]) copies the part of c from position 20 to the 0-terminator (inclusive) into the array d.Alethaalethea
Yes, that was what I was looking for! I kept getting the format wrong but I was close. Thank you!Jewelfish
T
4

strncpy is (essentially always) the wrong choice. In this specific case, it'll work, but only pretty much by accident -- since you're copying the end of a string, it'll work just like strcpy. That of course, leads to the fact that strcpy will work too, but more easily.

If you want to handle a more general case, there's a rather surprising (but highly effective) choice: sprintf can do exactly what you want:

sprintf(d, "%s", c+20);

sprintf (unlike strncpy or strcpy) also works for the most general case -- let's assume you wanted to copy just do I to d, but have d come out as a properly NUL-terminated string:

sprintf(d, "%*s", 4, c+13);

Here, the 4 is the length of the section to copy, and c+13 is a pointer to the beginning of the part to copy.

Tineid answered 20/9, 2012 at 0:50 Comment(2)
last example, doesn't work for me. It returns do I do this not do I d. ideone.com/u8HRuISeptet
I agree that sprintf is the best choice (I use it for all string copying operations) but I think snprintf is even better. Call me paranoid... I've gone one step further and defined myself snprintf_safe whose return value is limited to only valid numbers in the range of 0 to n-1 (where n is the buffer size). I have found a class of bugs which use the return value from snprintf, since it can return values longer than the actual string which was written - useful in some situations but dangerous when you aren't expecting it...Millet
S
0

Sounds like you are looking for the strncpy function. Don't mind the C++ site, the documentation is for C.

Or you can also use the strcpy function.

Stonecutter answered 20/9, 2012 at 0:30 Comment(1)
Beware: "No null-character is implicitly appended to the end of destination, so destination will only be null-terminated if the length of the C string in source is less than num."Letter
M
0

Use memcpy which may be a better fit, can only copy a portion of a string.

Using strncpy or strcpy actually copies a whole string up to the '\0' terminator.

In any case, it is prudent and wise, to assume there is plenty of space for the resulting string otherwise buffer overflows so ensure there is sufficient space in the buffer.

Malonis answered 20/9, 2012 at 0:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.