8.2.2.1. strlen

Time: 00:02:37 | Download: Large, Large (CC), Small | Streaming, Streaming (CC) | Slides: PDF, PPTX

Obtaining a string's length - the number of characters it currently stores - is a basic string operation. String objects support the dot operator, and most string classes provide a length function. However, C-strings, like all fundamental types, do not support the dot operator. In place of the .length() function defined by most string classes, the standard C-string library provides the strlen function, which counts and returns the number of characters in a C-string. The function does not count the null termination character or any characters following it.

strlen: String-Length

A C-string illustrated as a sequence of squares. The string s contains the characters 'EXAMPLE'. The first letter 'E' is at index location 0, 'X' is at 1, and so on until the last 'E' is at 6. The null terminator, '\0', is at location 7. The array is longer than the string, so unused space or elements follow the null terminator.

strlen(s)

Getting the length of a C-string. The length of a string is the number of characters currently stored in the string, not the total capacity of the string. The length of the string does not include the null termination character, so strlen(s); returns 7.
Header File:
#include <cstring>
Standard Prototype:
size_t strlen(const char* str);
Please see strlen for more information
Microsoft Prototype:
size_t strnlen(const char *str, size_t maxlen); 1
size_t strnlen_s(const char *str, size_t numberOfElements);
Please see strnlen and strnlen_s for more information
1 Also available on some Linux systems

strlen Examples

s.length()
Common strlen error. C-strings are primitive data, so they do not support the dot operator. Programmers cannot use the .length() function with any C-string.

 

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	char* s = "EXAMPLE";
	cout << strlen(s) << endl;

	char s[0] = '\0';
	cout << strlen(s) << endl;

	return 0;
}

Output:

7
0
The strlen function. The compiler automatically inserts a null termination character at the end of the string literal "EXAMPLE", but strlen doesn't count it as a part of the string's length and ignores any characters following it.

Possible strlen Implementations

Implementing a string as a fundamental data type involves balancing conflicting goals. C-strings prioritize structural simplicity and passing efficiency at the expense of some operations running more slowly. The C-string strlen counts the characters to obtain the string's length, resulting in a linear runtime. Fortunately, the counting loop is small and consists of a few machine instructions. Furthermore, optimizing compilers typically cache the function's return value (e.g., when driving a loop) if the string doesn't change. Programmers can implement the counting loop in many ways.

int strlen(const char* s)
{
	int	len = 0;

	while (s[len])
		len++;

	return len;
}
int strlen(const char* s)
{
	int	len = 0;

	while (*(s+len))
		len++;

	return len;
}
(a)(b)
int strlen(const char* s)
{
	int len = 0;

	for (; s[len]; len++)
		;

	return len;
}
int strlen(const char* s)
{
	int len = 0;

	for (; *(s+len); len++)
		;

	return len;
}
(c)(d)
Implementing strlen. The strlen function "walks" the C-string from left to right with a loop and counts each character. The null-termination character is equivalent to 0 and is interpreted as false, causing the loop to end.

Versions (a) and (c) use an indexing notation that may be more familiar and easier to understand. The (b) and (d) versions use address arithmetic and the dereference operator.