I did not (non yet) dig too deep into your code, but at first glance I see in case you get a match you account for all possible substrings (what currently fails). I do not understand what could be the reason of the else branch.
It steps into the else branch in the second loop repetition for cabc c, for abaabc c already in the first run. At that time your procedure does not "know" if there is any c in string, so there is no reason to already count something (implicit IMO). I double ne555's request.
I think this is the best i can explain to you.Feel free to ask any other query.
Tested your examples, tested three more, using your program shown (now) in OP. All restults are as expected. I would say: WAD (works as designed ... ah, no -- as desired).