In this C++ tutorial, you will learn how to find the length of a string using std::string::length(), std::string::size(), strlen(), and a loop. You will also learn when to use each method, whether the null terminator is counted, and how string length differs from the memory occupied by a string.

C++ String Length

The length of a string is the number of characters contained in it. For example, the string "save trees" has 10 characters: four letters in save, one space, and five letters in trees.

For a C++ std::string, use length() or size(). Both return the number of elements in the string. For a null-terminated C-style string stored in a character array, strlen() can be used instead.

1. C++ string length using string.length()

length() is a member function of string class that returns the length of this string.

In the following example program, we shall take a string, and find its length using length() function.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   string str = "save trees";
   int len = str.length();
   cout << "String Length : " << len << endl;
}

Output

String Length : 10
Program ended with exit code: 0

The returned value represents the number of characters stored in the std::string. Spaces and punctuation are counted because they are also characters in the string.

2. C++ string length using string.size()

size() is a member function of string class that returns the number of characters in this string.

In the following example program, we shall take a string, and find its length using size() function.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   string str = "save trees";
   int len = str.size();
   cout << "String Length : " << len << endl;
}

Output

String Length : 10
Program ended with exit code: 0

C++ string length() vs size()

For std::string, length() and size() have the same meaning. Both return the number of characters stored in the string.

</>
Copy
str.length()
str.size()

You can therefore use either form when you need the length of a std::string. length() often reads naturally when discussing text, while size() is consistent with other standard library containers such as std::vector. The returned type is std::string::size_type, which is commonly written as std::size_t when storing string sizes.

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

int main() {
    string str = "TutorialKart";

    cout << str.length() << endl;
    cout << str.size() << endl;

    return 0;
}

Output

12
12

3. C-style string length using strlen()

strlen() is a member function of cstring library. The function returns the number of characters between the beginning of the string and the terminating null character of the char array passed as argument.

In the following example program, we shall take a C style string, char array, and find its length using strlen() function.

To use strlen() function, you have to include cstring header file at the start of your program.

C++ Program

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

int main() {
   char str[] = "save trees";
   int len = strlen(str);
   cout << "String Length : " << len << endl;
}

Output

String Length : 10
Program ended with exit code: 0

strlen() is intended for null-terminated character sequences. It examines characters until it reaches the first '\0' character. For a normal std::string, prefer length() or size() instead of converting the string merely to call strlen().

Does C++ string length include the null terminator?

No. The length reported for ordinary text does not include the terminating null character.

For a C-style string such as char str[] = "abc";, the array contains four elements: 'a', 'b', 'c', and the terminating '\0'. However, strlen(str) returns 3, because it counts the characters before the null terminator.

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

int main() {
    char str[] = "abc";

    cout << "strlen: " << strlen(str) << endl;
    cout << "sizeof: " << sizeof(str) << endl;

    return 0;
}

Output

strlen: 3
sizeof: 4

Here, strlen() gives the text length, while sizeof(str) gives the size of the entire character array in bytes. Because each char occupies one byte, this array occupies four bytes, including its null terminator.

strlen() vs sizeof() vs string.size() in C++

These operations answer different questions and should not be used interchangeably.

  • std::string::size() and std::string::length() return the number of characters stored in a std::string.
  • strlen() counts characters in a null-terminated C-style string until the first null character.
  • sizeof reports the size in bytes of an object or type as seen by the compiler. It does not generally mean “string length.”

For example, applying sizeof to a std::string object returns the size of the string object itself, not the number of characters currently stored in that string.

4. C-style string length using While Loop

If you are just starting with C++ programming, it is always good to know the basics. You can find the length of the string using a while loop. This allows you to understand how a string is stored and the limits to identify a string.

In while loop, we start with the first character and continue with subsequent characters until we find null character \0.

In the following example program, we shall take a string, and find its length using C++ While Loop.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
    char str[] = "save trees";
    
    char *ch = str;
    int len = 0;
    while(*ch != '\0'){
        len++;
        ch++;
    }
   
    cout << "String Length : " << len << endl;
}

Output

String Length : 10
Program ended with exit code: 0

This loop follows the same basic idea as strlen(): begin with the first character and keep counting until the terminating null character is reached.

Find C++ string length using a for loop

A for loop can also count characters in a null-terminated character array. The loop continues until it reaches '\0'.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    char str[] = "save trees";
    int len = 0;

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

    cout << "String Length : " << len << endl;

    return 0;
}

Output

String Length : 10

For production code that already uses std::string, manually counting its characters is unnecessary. Use size() or length().

C++ string length with spaces and an empty string

Spaces are included in the string length. An empty string, on the other hand, has a length of zero.

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

int main() {
    string first = "C++ string";
    string second = "";

    cout << first.length() << endl;
    cout << second.length() << endl;

    return 0;
}

Output

10
0

The first value is 10 because the space between C++ and string is counted as one character.

String length and string size in bytes are not always the same concept

For std::string, size() returns the number of char elements stored in the string. This should not be confused with the total memory consumed by the std::string object and its storage.

There is another important distinction when text uses a multibyte encoding such as UTF-8. A user-visible character can require more than one char element. Therefore, std::string::size() reports the number of stored code units or bytes for UTF-8 text, not necessarily the number of characters a person sees on screen.

Time complexity of C++ string length operations

For modern std::string, size() and length() return the stored string size without scanning every character, so they operate in constant time.

strlen() works differently. It must scan a null-terminated character sequence until it finds '\0', so its running time grows with the number of characters examined. A manual loop that searches for the terminating null character has the same general behavior.

Choosing the correct C++ string length method

  • Use str.length() when working with a std::string and you want wording that directly expresses string length.
  • Use str.size() when working with a std::string, especially when writing code consistent with other standard containers.
  • Use strlen() for a valid null-terminated C-style string.
  • Use a manual loop mainly when learning how null-terminated strings are counted or when you need custom processing while traversing the characters.
  • Do not use sizeof as a replacement for string length unless you specifically need the storage size of an array or object.

C++ string length editorial QA checklist

  • Verify that examples using std::string use length() or size() rather than unnecessarily calling strlen().
  • Confirm that strlen() examples operate on properly null-terminated character sequences.
  • Do not describe sizeof as returning the number of characters in a general C++ string.
  • Verify that examples do not count the terminating '\0' as part of the text length.
  • When discussing std::string::size(), distinguish stored char elements from user-perceived characters in multibyte encodings.
  • Prefer an unsigned string-size type such as std::size_t when new examples store values returned by size() or length().

C++ string length summary

In this C++ Tutorial, we learned how to find the length of a string using builtin functions, other libraries, while loop, etc.