How to Find the Length of a String in C++

by | C++, Programming, Tips

A C++ string variable contains a collection of characters surrounded by double-quotes. There are two types of strings used in C++.

  • C-strings, which are arrays of type char terminated by the null character \0.
  • Strings that are objects of the Standard C++ Library string class.

This tutorial will go through the methods to find the length of a string in C++.


Find Length of String Using string::size

We can use the size() function to obtain the length of a string object. The size() function is a member function of the string class that returns the number of characters in the given string. Let’s look at an example:

#include <iostream>

using namespace std;

int main() {

    string str = "C++ is fun to learn!";

    cout << "String Length is: " << str.size();

    return 0;
}
String Length is: 20

Find Length of String Using string::length

The length() function is equivalent to the size() function. Let’s look at an example of using the length() function:

#include <iostream>

using namespace std;

int main() {

    string str = "C++ is fun to learn!";

    cout << "String Length is: " << str.length();

    return 0;
}
String Length is: 20

Find Length of String Using strlen()

We can use the strlen() function if we have a C-string string. The strlen() function is a member function of the C library string that returns the number of characters from the beginning of the char array to the terminating null character. Let’s look at an example:

#include <iostream>

using namespace std;

int main() {

    char str[] = "C++ is fun to learn!";

    cout << "String Length is: " << strlen(str);

    return 0;

}
String Length is: 20

Using While Loop

A while loop to determine the length of a string is a helpful way to understand how we define strings in C++. Let’s look at an example of using a counter within a while loop.

The loop starts from the first character, with each iteration, the counter and the array position increments by one.

The while loop terminates once we reach the null character \0.

#include <iostream>

using namespace std;

int main() {

    char str[] = "C++ is fun to learn!";

    char *ch = str;

    int count =0;

    while(*ch != '\0'){

        count++;

        ch++;

    }

    cout << "String Length is: " << count;

    return 0;

}
String Length is: 20

Using For Loop

We can also use a for loop to traverse a char array. Let’s look at an example of using a counter within a for loop to count each character up to the terminating null character.

#include <iostream>

using namespace std;

int main() {
    
    char str[] = "C++ is fun to learn!";

    int count = 0;

    for (int i = 0; str[i] != '\0'; i++){

        count++;

    }

    cout << "String Length is: " << count;

    return 0;

}
String Length is: 20

Summary

Congratulations on reading to the end of this tutorial! We have gone through five ways to get the length of a string.

For further reading on string manipulation in C++, go to the article: How to Reverse a String in C++.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee