hello all,
how to find string length without using any counter?
please provide me hints.
Thank You,
Nitin Mhetre.
hello all,
how to find string length without using any counter?
please provide me hints.
Thank You,
Nitin Mhetre.
Well, if we are talking about standar C strings, there's no other option - strings don't have a stored length, so there's no option but to "look through the string, and count up the number of characters until the end".
But you don't have to write the code for it, as C standard string library contains the function strlen, which gives you the number of characters in the string (note: not the counting the zero byte at the end, so if you use to to figure out how much emmoery you need for a particular string, you need one additional character to indicate the end, so you need to add one to the length).
You need to use--Code:#include <string.h>
Mats
Hint: use recursion
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Another way to avoid a counter is to take the address of the first character and then keep incrementing it until you hit a '\0'. Then take the address of that character and subtract the first from the second, giving you the number of characters in the string.
I in *no* way suggest that as an actual approach! ;-)
QuantumPete
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd