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
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
#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:
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.