C++ Strings : length(), size()

String Length

To get the length of a string, use can use size() or length() function

Syntax:
string.size();
string.length();

Example:1
string n1=”Hello”;
cout<<“length “<<n1.size()<<endl;
cout<<“length “<<n1.length()<<endl;

Example:2
n1=”Hello World”;
cout<<“length “<<n1.size()<<endl;
cout<<“length “<<n1.length()<<endl;

//length of string size(), length()
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string n1="Hello";
	cout<<"length "<<n1.size()<<endl;
	cout<<"length "<<n1.length()<<endl;
	
	n1="Hello World";
	cout<<"length "<<n1.size()<<endl;
	cout<<"length "<<n1.length()<<endl;
	
	return(0);
}

Output:

length 5

length 5

length 11

length 11