![]() |
VOOZH | about |
The string is a sequence of characters or an array of characters. The declaration and definition of the string using an array of chars are similar to the declaration and definition of an array of any other data type.
Examples:
Input: "Geeksforgeeks" Output: 13 Input: "Geeksforgeeks \0 345" Output: 14
There are few methods to find the length of a string is mentioned below:
The method string::size returns the length of the string, in terms of bytes.
13
The method string::length returns the length of the string, in terms of bytes. Both string::size and string::length are synonyms and return the exact same value.
13
The C library function size_t strlen(const char *str) computes the length of the string str up to, but not including the terminating null character.
13
Using the traditional method, initialize the counter equals 0 and increment the counter from starting of the string to the end of the string (terminating null character).
13
To initialize the counter equals 0 and increment the counter from starting of the string to the end of the string (terminating null character).
13
Time complexity: For all the methods, the time complexity is O(n) as we need to traverse the entire string to find its length.
Space complexity: For all the methods, the space complexity is O(1) as no extra space is required.