Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Strlen() in C++?
2.1.
Syntax of strlen()
2.2.
Parameters of strlen() in C++
2.3.
Return Value of strlen() in C++
3.
Working with strlen()
4.
Example of strlen() in C++
4.1.
C++
5.
strlen() Prototype
6.
strlen() Undefined Behavior
7.
A Closer Look at strlen()
8.
Potential Pitfalls
9.
More Examples of strlen() in C++
9.1.
1. Program to show the mechanism of the strlen() function
9.2.
C++
9.3.
2. Program to check if the length of two strings is equal or not using the strlen() function
9.4.
C++
10.
Frequently Asked Questions
10.1.
What does the strlen() function do in C++?
10.2.
Does strlen() count the null character?
10.3.
Does strlen() check the array bounds?
11.
Conclusion
Last Updated: May 5, 2024
Easy

strlen() in C++

Author Gunjan Batra
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Hello to all the code enthusiasts out there! Today, we'll be taking a closer look at the strlen() function in C++, a vital tool for handling strings. 

strlen() in C++

We'll explain what it does, how to use it, and where it fits into your C++ programming toolkit.

What is Strlen() in C++?

strlen(), which stands for string length, is a standard library function in C++ that is used to find the length of a string. The length of a string is determined by the number of characters before the null character (\0).

Syntax of strlen()

Here's the basic syntax of the strlen() function:

size_t strlen(const char *str);

In this syntax, str is the string whose length we need to find. The function returns the length of the string.

Parameters of strlen() in C++

The strlen() function in C++ accepts a single parameter. str is a null-terminated string (character array) whose length is to be determined.

Return Value of strlen() in C++

The strlen() function in C++ returns the number of characters in the null-terminated string str, excluding the null character ('\0').

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Working with strlen()

Let's look at a simple example of how strlen() works:

#include <iostream>
#include <cstring>


int main() {
    char str[] = "Hello, World!";
    std::cout << "The length of the string is: " << strlen(str);
    return 0;
}

In this code, strlen() calculates the length of the string "Hello, World!" (excluding the null character), and the output will be 13.

Output

Output

Example of strlen() in C++

  • C++

C++

#include <iostream>
#include <cstring>

int main() {
char str[] = "Hello, World!";
int length = strlen(str);
std::cout << "Length of the string: " << length << std::endl;
return 0;
}

Output

Length of the string: 13

 

This example demonstrates the usage of the strlen() function to determine the length of a null-terminated string in C++.

strlen() Prototype

The prototype of strlen() function in C++ is:

size_t strlen(const char* str);

Here, str is a pointer to a null-terminated string whose length is to be determined. The function returns a value of type size_t, representing the number of characters in the string, excluding the null character ('\0').

strlen() Undefined Behavior

If the str argument passed to strlen() is not a null-terminated string or if it points to memory that is not accessible, the behavior of the function is undefined. This can lead to unpredictable results, including crashes or incorrect length calculations. It's essential to ensure that the input string is properly null-terminated and valid.

A Closer Look at strlen()

It's crucial to note that strlen() doesn't count the null character that marks the end of the string. If you have a string "Hello" (5 characters) in C++, the null character is automatically appended at the end, making the actual array size 6. But strlen() would return 5.

Another important aspect is that strlen() computes the string length at runtime, not at compile time. This means that if the string content changes, strlen() will return the updated length.

Potential Pitfalls

While strlen() is quite useful, it doesn't perform any array bounds checking. If the given string doesn't contain a null character, strlen() will keep counting until it finds one, possibly leading to buffer overflows. 

Also read -  File Handling in CPP

More Examples of strlen() in C++

1. Program to show the mechanism of the strlen() function

  • C++

C++

#include <iostream>
#include <cstring>

int main() {
char str[] = "We are ninjas and we write code";
int length = strlen(str);
std::cout << "Length of the string: " << length << std::endl;
return 0;
}

Output

Length of the string: 31

 

In this example, the strlen() function is used to determine the length of the string "We are ninjas and we write code". The result is then printed to the console.

2. Program to check if the length of two strings is equal or not using the strlen() function

  • C++

C++

#include <iostream>
#include <cstring>

int main() {
char str1[] = "Hello";
char str2[] = "World";

if (strlen(str1) == strlen(str2)) {
std::cout << "Length of both strings is equal." << std::endl;
} else {
std::cout << "Length of both strings is not equal." << std::endl;
}

return 0;
}

Output

Length of both strings is equal.

 

In this example, the strlen() function is used to determine the lengths of two strings str1 and str2. The lengths are then compared, and a message indicating whether they are equal or not is printed to the console.

Frequently Asked Questions

What does the strlen() function do in C++?

The strlen() function calculates the length of a string, excluding the null character.

Does strlen() count the null character?

No, strlen() doesn't count the null character. It counts only the characters before the null character.

Does strlen() check the array bounds?

No, strlen() doesn't check array bounds, which could lead to buffer overflows if the string doesn't contain a null character.

Conclusion

Understanding the strlen() function is crucial when dealing with strings in C++. It allows you to calculate the string length at runtime. However, remember to always ensure your string contains a null character to avoid potential buffer overflows. 

Recommended articles:


We hope this blog helped you to understand the concept of the Member Function in C++. You can refer to our guided paths on the Coding Ninjas Studio platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Happy Learning!! 

Live masterclass