String Length Function - C++ Programming Tutorial
C++ Course / Strings / String Length Function

String Length Function

BLUF: Mastering String Length Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: String Length Function

C++ is renowned for its efficiency. Learn how String Length Function enables low-level control and high-performance computing in the tutorial below.

C++ String length

This function is employed to determine the size of the string in terms of bytes.

This represents the precise number of bytes comprising the string content, which may not necessarily match the storage capacity.

Syntax

Example

int len = s1.length();

Parameters

This function contains single parameter.

Return Value

This function provides the integer value represented in bytes.

Here, s1 represents a string, and the strlen function determines the string s1's length, saving this value in the len variable.

Example 1

Example

#include<iostream>
using  namespace std;
int main()
{
string s1 = "Welcome to javacpptutorial";
int len = s1.length();
cout<< "length of the string is : " << len;
return 0;
}

Output:

Output

Length of the string is 21

Define a string variable s1 with the value "Welcome to javacpptutorial". Determine the length of this string by utilizing the strlen function and then save this length in a variable named len.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience