This question already has an answer here:
- Strings in C, how to get subString 10 answers
For example, I have this
char *buff = "this is a test string";
and want to get "test"
. How can I do that?
This question already has an answer here:
For example, I have this
char *buff = "this is a test string";
and want to get "test"
. How can I do that?
Assuming you know the position and the length of the substring:
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.
Use
char* strncpy(char* dest, char* src, int n)
from<cstring>
. In your case you will need to use the following code:Full documentation on the
strncpy
function here.You can use
strstr
. Example code hereNote that the returned result is not null terminated.
Job done :)
You can just use
strstr()
from<string.h>
$ man strstr