C++ String Length

To get the length of a string, you use the .length() or .size() member functions.

Example:


#include <iostream>
#include <string>
using namespace std;

int main() {

    string str = "Hello Friends!";
    // length of the string
    cout << str.length() << "\n";
    
    return 0;
}

Output:

14

Use size() to get the length of the string.


#include <iostream>
#include <string>
using namespace std;

int main() {

    string str = "Hello Friends!";
    // length of the string
    cout << str.size() << "\n";
    
    return 0;
}

Output:

14