Find Length of a String in C++

  1. Use the length Function to Find Length of a String in C++
  2. Use the size Function to Find Length of a String in C++
  3. Use the while Loop to Find Length of a String in C++
  4. Use the std::strlen Function to Find Length of a String in C++

This article will explain several methods of how to find the length of a string in C++.

Use the length Function to Find Length of a String in C++

The C++ standard library provides the std::basic_string class to augment char-like sequences and implement a generic structure for storing and manipulating such data. Although, most people are more familiar with std::string type which is itself type alias for std::basic_string<char>. std::string provides length built-in function to retrieve the length of the stored char sequence.

#include <iostream>
#include <cstring>

using std::cout; using std::cin;
using std::endl; using std::string;

int main(int argc, char *argv[]){
    string str1 = "this is random string oiwaoj";

    cout << "string: " << str1 << endl;
    cout << "length: " << str1.length() << endl;

    exit(EXIT_SUCCESS);
}

Output:

string: this is random string oiwaoj
length: 28

Use the size Function to Find Length of a String in C++

Another built-in function included in the std::string class is size, which behaves similarly to the previous method. It takes no arguments and returns the number of char elements in the string object.

#include <iostream>
#include <cstring>

using std::cout; using std::cin;
using std::endl; using std::string;

int main(int argc, char *argv[]){
    string str1 = "this is random string oiwaoj";

    cout << "string: " << str1 << endl;
    cout << "length: " << str1.size() << endl;

    exit(EXIT_SUCCESS);
}

Output:

string: this is random string oiwaoj
length: 28

Use the while Loop to Find Length of a String in C++

Alternatively, one can implement his/her own function to calculate the length of the string. In this case, we utilize the while loop to traverse the string as a char sequence and increment the counter by one each iteration. Notice that the function takes char* as an argument, and the c_str method is called to retrieve this pointer in the main function. The loop stops when the dereferenced pointer value is equal to 0, and the null-terminated string implementation guarantees that.

#include <iostream>
#include <cstring>

using std::cout; using std::cin;
using std::endl; using std::string;

size_t lengthOfString(const char *s)
{
    size_t size = 0;

    while (*s) {
        size += 1;
        s +=1;
    }

    return size;
}

int main(int argc, char *argv[]){
    string str1 = "this is random string oiwaoj";

    cout << "string: " << str1 << endl;
    cout << "length: " << lengthOfString(str1.c_str()) << endl;

    exit(EXIT_SUCCESS);
}

Output:

string: this is random string oiwaoj
length: 28

Use the std::strlen Function to Find Length of a String in C++

Finally, one can resort to the old-school C string library function strlen, which takes a single const char* argument as our custom-defined function - lengthOfString. These last two methods can be faulty when called on char sequences that don’t end with a null byte, as it could access out-of-range memory during the traversal.

#include <iostream>
#include <cstring>

using std::cout; using std::cin;
using std::endl; using std::string;

int main(int argc, char *argv[]){
    string str1 = "this is random string oiwaoj";

    cout << "string: " << str1 << endl;
    cout << "length: " << std::strlen(str1.c_str()) << endl;

    exit(EXIT_SUCCESS);
}

Output:

string: this is random string oiwaoj
length: 28
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ String

  • Compare Two Strings Ignoring the Case in C++
  • Concatenate Two Strings in C++