C++ Standard Library C++ STL Library

C++ <cstring> - strlen() Function



The C++ <cstring> strlen() function returns the length of a byte string, which is, the number of characters in the byte string whose first element is pointed to by str (without including the terminating null character).

The behavior is undefined if there is no null character in the character array pointed to by str.

Syntax

size_t strlen ( const char * str );

Parameters

str Specify pointer to the character array.

Return Value

Returns the length of a byte string.

Example:

The example below shows the usage of strlen() function.

#include <iostream>
#include <cstring>
using namespace std;
 
int main (){
  char str1[50] = "Hello World!";
  char str2[50] = "Programming is easy.";

  cout<<"str1 contains: "<<strlen(str1)<<" characters.\n";
  cout<<"str2 contains: "<<strlen(str2)<<" characters.\n";   
  return 0;
}

The output of the above code will be:

str1 contains: 12 characters.
str2 contains: 20 characters.

❮ C++ <cstring> Library