Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!
length() function is a built-in function in C++ that returns the length of a string. It can be used with any string variable or string literal.
Here is an example code that demonstrates how to use the
using namespace std;
string str = "Hello World!";
int len = str.length();
cout << "The length of the string is: " << len << endl;
In this example, we first declare a string variable
str and assign it the value “Hello World!”. We then use the
length() function to find the length of the string and store it in an integer variable
len. Finally, we print the length of the string using the
length() function in C++ is a simple and effective way to find the length of a string. It can be used in a variety of applications, from simple string manipulation to more complex algorithms.