C++ Program to Find Length of String

Here we will see various ways to find length of string in C++. We can do this in two ways, one is by using inbuilt functions such as length(), and size(), and another way is to find using a loop manually.

Method 1: Using length() or size()

#include <iostream>

using namespace std;

int main() {
    string str = "The Crazy Programmer";
    
    cout << "Length of the string is: " << str.length() << endl;
    cout << "Length of the string is: " << str.size();
   
    return 0;
}

Output:

Length of the string is: 20
Length of the string is: 20

Method 2: Using Loop

We can use any loop to iterate through the string to find its length.

#include <iostream>

using namespace std;

int main() {
    string str = "Hello World";
    int i;
    
    for(i = 0; str[i]; i++);
    cout << "Length of the string is: " << i;

    return 0;
}

Output:

Length of the string is: 11

7 thoughts on “C++ Program to Find Length of String”

    1. I guess you did not read the title of the blog. It clearly says “Without using Library functions”. The function “strlen()” is library function.

Leave a Comment

Your email address will not be published. Required fields are marked *