unsignedint str_search(constchar *pattern, constchar *text) {
unsignedint counter = 0;
/*int length = 0;
int length( char*pattern){
for (i = 0 ; pattern [i] != '\x0' ; i++){
length++;
}
}
*/
for (int i = 0 ; text[i] != '\x0'; i++){
if (text[i] == *pattern){
counter++;
}else {
i++;
}
}
return counter;
}
I'm thinking of iterating through the text and look through the occurrences of the word. I'm having trouble thinking how to compare/look for the word while i is going through the string.
'\X0' = 0 ... typing 5 characters for 1 does not improve readability.
you can't compare strings like that.
what I would do, roughly, is this:
for(all the letters) //optimize tweak, you can skip the difference in lengths as a pattern 3 long can't fit in 1 char at the end of the test string,right? this is critical to avoid memory out of bounds issues!!!
{
for(all the letters in pattern)
do they all match? if yes, found it, mark location and continue
}
so, imagine pattern aaa and input abaaabaaaa
this will check aaa against aba, and fail then increment by 1 in outer loop. now check baa against aaa. nope. increment 1, check aaa vs aaa, yes, found 1. now check aab, no, aba, no, baa, no, aaa, yes, aaa, yes, string to short to hold aaa, stop.
see how that handles the case of 2 copies of the pattern at the end? It handles all the edge cases because it checks every possible way. Its brute forced, but it works.
to make a long story short, you need 2 loops to do this (the simple code way). There is probably a hard to read and understand single loop version.