8.2.2.1. strlen

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

Regardless of how we implement a string, determining how many characters are in it is one of the most basic string operations. The strlen function counts and returns the number of characters in a C-string. Note that it does not include in the count either the null termination character or any characters between the null termination and the end of the character array. Also note that, unlike Java strings, you cannot use .length() to determine the length of a C-string.

strlen: String-Length

A C-string illustrated as a sequence of squares. The string is named s and 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

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 Implementations

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

	while (s[len])		// '\0' ends the loop
		len++;

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

	while (*(s++))		// '\0' ends the loop
		len++;

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

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

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

	for (; *(s+i); i++)
		;

	return i;
}
(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 is the character equivalent of zero and is interpreted as false, ending the loop.

Versions (a) and (c) use an indexing notation that may be more familiar and easier to understand. The (b) and (d) versions use the dereference operator. Note that version (b) changes the value stored in s; for this reason, the program cannot use s for any other string operations after the loop.