That's because of that return inside the loop. It causes the function to exit immediately so the loop doesn't have a chance to repeat more than one time.
For any non-empty string, it will check if the first character is the terminator, then increment
i (so now
i = 1) and index into the string (but do nothing with the character), then return
i (which is always 1).
You want to return only after you found the terminator, so you only need one return after the loop. The loop itself should only check if the character currently examined is the terminator, and, if it's not, increment the index to examine the following character during the next iteration.
Incidentally, the index is also the count of the characters excluding the terminator, so returning
i is sufficient to make the function work. In the case where a string is empty ("\0", meaning it only contains the terminator) the condition of the loop is immediately false,
i doesn't get incremented, and
return i
will return 0.
Again, thank you maeriden for your input and patience |
No problem, that's why I hang around here. But if you are tired I don't recommend you work on this. It's a lot harder to make sense of things.