Signup/Sign In

C++ Program to find Length of the string

In this tutorial, we will how to find length of the string starting from the first index to the last index.

Program to Find Length of a String in C++

Before moving to the programming let's have a look on what exactly is the statement.

You can get the length of a string object by using a size() function or a length() function.

The size() and length() functions are just synonyms and they both do exactly same thing.

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

int main() {
    char str[] = "C++ Programming is awesome";

    cout << "String Length = " << strlen(str);

    return 0;
}


String Length = 15

#include <iostream>
using namespace std;

int main() {
    string str = "C++ Programming";

    // you can also use str.length()
    cout << "String Length = " << str.size();

    return 0;
}


String Length = 15

Conclusion

Here, in this tutorial, we have learned different approaches on how to find the length of any string entered by the user.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.