Can anyone explain to me how to call the following function to find the length of an array? Thanks!
int len(char* s)
{
int k = 0;
while (*s)
{
k++;
s++;
}
return k;
}
This function will not help you find the length of just any array - only some very specific ones:
char,In C arrays like that are created when you do this:
char array[] = "12345";
This is a six-element character array, with five initial elements occupied by non-zero character codes, and the sixth element occupied by zero.
Now the algorithm becomes clear: count elements until you hit zero, include zero in the count, and return the result:
char array[] = "12345";
int res = len(array);
len(s)wheresis a pointer to a char array.strlen()though, which gives you the length of a C-style string. Using C-style strings is the first thing to get rid of when learning C++ though...