hello all,
how to find string length without using any counter?
please provide me hints.
Thank You,
Nitin Mhetre.
Printable View
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
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