180

For example, I have this

char *buff = "this is a test string";

and want to get "test". How can I do that?

J0e3gan
  • 8,740
  • 10
  • 53
  • 80

5 Answers5

258
char subbuff[5];
memcpy( subbuff, &buff[10], 4 );
subbuff[4] = '\0';

Job done :)

nobody
  • 19,814
  • 17
  • 56
  • 77
Goz
  • 61,365
  • 24
  • 124
  • 204
  • 22
    This will waste memory. You have been warned! :P – alexandernst Nov 11 '13 at 19:00
  • 27
    @alexandernst: How exactly? 5 bytes allocated on the stack (which will get freed when it drops out of scope) is hardly wasting memory ... – Goz Nov 11 '13 at 21:38
  • 1
    Still more than what you need, but yeah, they'll get free-ed as soon as the code leaves the current stack. – alexandernst Nov 11 '13 at 21:39
  • 13
    @alexandernst: The problem is that you can't do much with that string data without copying it. If you take a pointer to the section from the original string you need to know the length as it will have no null terminator (Without affecting the original string). – Goz Nov 12 '13 at 06:56
  • 38
    Don't forget to #include . – Juan Pablo Rinaldi Jan 18 '14 at 04:03
  • @Juampi: Is string.h really needed? You're only using arrays and memcpy. – TNT May 27 '20 at 15:16
  • @alexandernst: Couldn't you reuse the same buff? `memcpy(buff, &buff[10], 4);` `buff[4] = '\0';` – TNT May 27 '20 at 15:22
  • 1
    @TNT writing to pointer to string literal is undefined behavior. Your code crashes the program on my machine. See [this](http://c-faq.com/decl/strlitinit.html). – Ekrem Dinçel Sep 10 '20 at 10:12
  • 1
    subbuff[4] = '\0'; this is very important, thanks for answer. – Good man Sep 27 '20 at 17:38
108

Assuming you know the position and the length of the substring:

char *buff = "this is a test string";
printf("%.*s", 4, buff + 10);

You could achieve the same thing by copying the substring to another memory destination, but it's not reasonable since you already have it in memory.

This is a good example of avoiding unnecessary copying by using pointers.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
  • @procrastinator: Not really only for printing. This is a viable approach in case the substring is not going to be modified. This way you simply keep a pair of pointers instead of allocating a variable-length buffer. – Blagovest Buyukliev Oct 07 '15 at 07:19
  • 1
    N̶i̶c̶e̶ ̶o̶n̶e̶ ̶f̶o̶r̶ ̶p̶r̶i̶n̶t̶i̶n̶g̶ ̶o̶n̶l̶̶̶̶y̶ Sorry if you got me wrong, I mean that your solution doesn't require an extra string, which is perfect when you only need to print :-) –  Oct 08 '15 at 05:12
  • 7
    use sprintf to store the above formatted string – Parnab Sanyal Aug 09 '16 at 18:53
  • @ParnabSanyal how can I use sprintf to store the substring? Do I need to pass substring length as third parameter? – Verma Aman Nov 11 '18 at 14:56
  • 1
    @VermaAman no you don't. Your first argument would be the resulting string, second argument would be the source string and if you have placeholders inside second argument like `%c`, `%i` etc then from third argument to upto the number of placeholders, would be the actual values of the placeholder( comma separated). Just like `printf` – Parnab Sanyal Nov 11 '18 at 15:13
70

Use char* strncpy(char* dest, char* src, int n) from <cstring>. In your case you will need to use the following code:

char* substr = malloc(4);
strncpy(substr, buff+10, 4);

Full documentation on the strncpy function here.

Community
  • 1
  • 1
Mihai Scurtu
  • 1,448
  • 1
  • 11
  • 22
  • 40
    don't forget to add the null-termination character '\0' if you do this. – evandrix Jan 26 '13 at 08:01
  • 15
    don't forget to malloc the substr large enough if you do this. – FUD Sep 18 '15 at 05:31
  • ``` char *substr(char const *s, size_t start, size_t length) { char *substr = calloc(length + 1, sizeof(char)); strncpy(substr, s + start, length); return substr; } ``` – VDVLeon Oct 28 '22 at 17:39
7

You can use strstr. Example code here.

Note that the returned result is not null terminated.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Milan
  • 15,389
  • 20
  • 57
  • 65
6

You can just use strstr() from <string.h>

$ man strstr

Paul R
  • 208,748
  • 37
  • 389
  • 560