strlen() Function

The strlen() function in C calculates the number of characters in a string, excluding the terminating null character.


Syntax of strlen()

</>
Copy
size_t strlen(const char *str);

Parameters

ParameterDescription
strA pointer to the null-terminated C string whose length is to be determined.

Return Value

The function returns the number of characters in the string, not including the terminating null character.

It is important to note that strlen() measures the length of the string up to the first null character and does not reflect the total allocated size of the array holding the string. For example, an array declared with a size of 100 may contain a string of only 11 characters, which is what strlen() will report.


Examples for strlen()

Example 1: Basic Usage of strlen() to Determine the Length of a String

This example demonstrates the basic use of strlen() to calculate the length of a given string.

Program

</>
Copy
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello, world!";
    size_t len = strlen(str);
    printf("Length of the string: %zu\n", len);
    return 0;
}

Explanation:

  1. The string "Hello, world!" is stored in the array str.
  2. The strlen() function scans the array until it reaches the null terminator and counts the characters, resulting in a length of 13.
  3. The length is printed using printf(), displaying the result.

Output:

Length of the string: 13

Example 2: Comparing strlen() with sizeof to Highlight Their Differences

This example illustrates how strlen() returns the actual length of the string, while sizeof returns the total size of the allocated character array.

Program

</>
Copy
#include <stdio.h>
#include <string.h>

int main() {
    char mystr[100] = "test string";
    size_t length = strlen(mystr);
    size_t size = sizeof(mystr);
    
    printf("Length using strlen: %zu\n", length);
    printf("Size of array using sizeof: %zu\n", size);
    return 0;
}

Explanation:

  1. The character array mystr is declared with a size of 100 characters and initialized with the string "test string".
  2. strlen() calculates the length of the string by counting characters up to the null terminator, which results in 11.
  3. sizeof returns the total memory allocated for mystr, which is 100 bytes.
  4. This demonstrates that strlen() gives the logical length of the string, while sizeof provides the physical size of the array.

Output:

Length using strlen: 11
Size of array using sizeof: 100

Example 3: Iterating Over Each Character in a String Using strlen()

This example shows how to use the length returned by strlen() to iterate over and print each character of a string individually.

Program

</>
Copy
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Apple";
    size_t len = strlen(str);
    
    for (size_t i = 0; i < len; i++) {
        printf("Character at index %zu: %c\n", i, str[i]);
    }
    return 0;
}

Explanation:

  1. The string "Apple" is stored in the array str.
  2. The strlen() function determines the length of the string.
  3. A for-loop uses this length to iterate over each character of the string.
  4. Each character, along with its index, is printed using printf().

Output:

Character at index 0: A
Character at index 1: p
Character at index 2: p
Character at index 3: l
Character at index 4: e