c take a slice of an array by advancing the array -
i've got array of binary data in need count amount of times char sequence appears. @ moment defined function, find index of 'needle' in 'haystack'
int indexof(const char *needle, int needlelen, const char *haystack, int haystacklen) { /.../ }
it returns -1
if needle can't found, otherwise returns index.
the next step run indexof
in while
-loop (till -1
) count.
my question is: whenever found index in haystack
, how take slice of haystack
first char's determined indexof
have been chomped off?
example: haystack hello_world
, needle o
. indexof
return 4
. how take slice of hello world
without first 4
chars? e.g. want _world
my haystack hello_world, needle o. indexof return 4. how take slice of hello world without first 4 chars? e.g. want _world
if index 4, need skip first 5 characters (remember indexing in c 0 based).
that haystack + 4 + 1
. remember update size of haystack
accordingly, not access off end it.
or in c:
idx = indexof(needle, needlelen, haystack + idx + 1, haystacklen - idx - 1);
where if idx
4, index 5 onward.
Comments
Post a Comment