How to get the length of a string in C++
In this tutorial, we are going to learn about two different ways to find the length of a string in C++.
Using the length() function
In c++, we can use the built-in length() function to get the length of a given string which is defined inside the string.h header file.
Length means the total number of characters in a given string.
Here is an example, that gets the length of a string gowtham:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string user = "gowtham";
int length = user.length();
cout << "The length of a user string is: " << length;
return 0;
}Output:
The length of a user string is: 7Similarly, we can also use the size() function to get the length of a string in C++.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string user = "gowtham";
int length = user.size();
cout << "The length of a user string is: " << length;
return 0;
}

