strchr would do a smaller loop:
ptr = str;
while ((ptr = strchr(ptr '/')) != NULL)
count++, ptr++;
I must add that I do not approve of brevity for the sake of brevity, and I always choose the clearest expression, ceteris paribus. I believe that the cycle is strchrmore elegant, but the original implementation in the question is clear and lives inside the function, so I do not prefer one over the other if they both pass unit tests.
source
share