Finding the length of a string

Finding the length of a string

How to:

C++ provides a straightforward way to find a string’s length using the length() method of the std::string class. But if you’re old school, you can still go with C-style strings and strlen(). Here’s both in action:

#include <iostream>
#include <string>
#include <cstring>

int main() {
    // Using std::string
    std::string greeting = "Hello, World!";
    std::cout << "Length of string (std::string): " << greeting.length() << std::endl;

    // Using C-style string
    const char *c_greeting = "Hello, World!";
    std::cout << "Length of string (C-style): " << strlen(c_greeting) << std::endl;

    return 0;

Sample output:

Length of string (std::string): 13
Length of string (C-style): 13

Deep Dive:

Originally, C++ inherited C-style character arrays and the accompanying strlen() function from C. strlen() calculates the length by marching through the array until it hits the null character, '\0'. This is a simple yet effective strategy but it can’t beat the efficiency of std::string.length(), which typically keeps track of the length for quick retrieval.

Alternatives? Sure thing:

  • You can also use size() method, identical to length() for std::string.
  • For wide character strings, std::wstring and its length() method are your friends.
  • Spicier choices include custom functions or using algorithms like std::distance with iterators.

Beware though, std::string::length() returns a size_t type, an unsigned integer, which can trip you up with unexpected behaviors if you mix it with signed types in expressions.

See Also: