
Help about string & sub-string
The only problem with this is that it will not find overlaping strings.
Then again it's up to you if you want to allow them or not. For example,
if you're looking for the string
"huhhuh, inside "huhhuhhuh"
will return 1 instead of 2 (if that's what you wanted). The fix is simple
and it is left as an excercise to the reader.
Also, I don't see how strtok() would help.
Roberto
: : > I need to know how many times a certain sustring is in a bigger string. How
: : > can I implement thi feature with a C function.
: : > TIA
: : You could achieve this with strtok(), look it up in your man pages,
: : it's probably just the thing you were looking for.
: I don't readily see how strtok() would be useful for this. strstr() in a
: loop could do it, like this:
: #include <stdio.h>
: #include <string.h>
: main(int argc, char **argv)
: { char *sp, *si;
: int len, count;
: if (argc < 3)
: { printf("usage: $ %s \"substring\" \"hoststring\" [overlap]\n"
: , argv[0]);
: exit(0);
: }
: if (argc > 3 && !strcmp("overlap", argv[3]))
: len = 1;
: else
: len = strlen(argv[1]);
: si = argv[2];
: count = 0;
: while (sp = strstr(si, argv[1]))
: { count++;
: si = sp + len;
: }
: printf("%s\n occurs %d times in\n%s\n", argv[1], count, argv[2]);
: return 0;
: }
: --
--
----------------------------------------------------------------------