Select Page

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!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!