How would you get the number of characters from an array?

Such as, if I had the user input a character sequence ranging from 0-10. Is there a function that I could use with the array to return an integer value corresponding to the number of characters in the array? Including whitespaces?
If the array is a c-style string (i.e. an array of characters, with the end of the string marked by a zero value) you can use strlen.

Shamelessly stolen from wiki:
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <string.h>
 
int main(void)
{
    char *string = "Hello World";
    printf("%lu\n", (unsigned long)strlen(string));
    return 0;
}

This program will print the value 11, which is the length of the string "Hello World". 
1
2
3
4
5
template <typename T, size_t N>
inline size_t sizeof_array( T const (&a) [ N ] )
{
  return N;
}

Shamelessly stolen from this forum
@stereoMatching that's the capacity of the array.
@Hilo890 modify the code that Moschops gave you, to count the letters.
Last edited on
Thanks ne555, I haven't read the question carefully.
Thank you for your remind
@stereoMatching

If it's any consolation, after correcting you ne55 went on to answer a question that wasn't asked, involving counting letters (the OP wants to include whitespace in the count). ;)
Topic archived. No new replies allowed.