Finding occurrences of substring

I need to find the number of times a word or pattern appears in a string.
Note: STL containers and built-in functions are not allowed.

For example: If the pattern is "ana" and the string is "Ana loves bananas", the occurrence would be 3.

I'm new to programming so i've been struggling a lot. This is what i got so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  unsigned int str_search(const char *pattern, const char *text) {
unsigned int 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.
Last edited on
'\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.
Last edited on
There is probably a hard to read and understand single zero loop version.

Yep!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cctype>
using namespace std;

bool sameWord( const char *pattern, const char *text )
{
   if ( !(*pattern) ) return true;
   return tolower( *text ) == tolower( *pattern ) ? sameWord( pattern + 1, text + 1 ) : false;
}

int occurs( const char *pattern, const char *text )
{
   return *text ? sameWord( pattern, text ) + occurs( pattern, text + 1 ) : 0;
}

int main()
{
   cout << occurs( "ana", "Ana loves bananas" );
}

Last edited on
Topic archived. No new replies allowed.