Finding the position of a substring in a larger string

后端 未结 3 818
既然无缘
既然无缘 2020-12-11 23:46

I have created a function that should find the numerical position of the first character of a substring in a larger string. I am having some problems with the output and I a

3条回答
  •  不思量自难忘°
    2020-12-12 00:11

    Your loops are reversed. The outer loop should walk positions from zero to originalLength, inclusive; the nested loop should walk positions from zero to toFindLength, inclusive.

    Both originalLength and toFindLength should be set to values returned by strlen, not strlen plus one, because null terminator position is not a good start.

    Finally, you are returning -1 from inside the outer loop. This is too early - you should be returning -1 only after you are done with the outer loop as well.

提交回复
热议问题