In this article, we are going to learn different ways of how to find the length of the string in C++. We will learn about some built-in functions that are available in string class and also we will learn to do this by using custom functions using native C++ functions.
Ways to find length of a string in C++
- By Using the Loop to Find Length of String in C++.
- By Using the std::strlen function to find length of String in C++.
- Use the length Function to Find Length of String in C++.
- Use the size Function to Find Length of a String in C++.
1.Using Loop to Find Length of String in C++
The first method that we are going to learn is by using the C++ loops. In this method, we will use a loop to iterate
over a string and then count the length of this string. We will use the standard C++ pointer concept in this iteration. We will iterate the string until we reach the end of the string. The end of the string can be identified when the pointer reaches the end and points to either 0 or null character. Let us understand this with the help of an example.
C++ Program to find Length of String using loop
#include <iostream>
#include <cstring>
using namespace std;
size_t GetStringlengh(const char* s)
{
size_t size = 0;
while (*s) {
size += 1;
s += 1;
}
return size;
}
int main() {
string str = "This is string length tutorial";
size_t strlen = 0;
strlen = GetStringlengh(str.c_str());
cout << "The length of the String is : " << strlen << endl;
return 0;
}
Output
The length of the String is : 30
2. Using the std::strlen function to find length of String in C++
The next method that we are going to learn is by using the strlen() function of C++. We can pass the string to this function and this function returns the string length. Here is an example.
C++ Program to find Length of String using std::strlen
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string str = "This is string length tutorial";
size_t strln = 0;
strln = std::strlen(str.c_str());
cout << "The length of the String is : " << strln << endl;
return 0;
}
Output
The length of the String is : 30
3.Use length Function to Find Length of a String in C++
Another method that we are going to learn is the length() function. String class of std library provides us length()
function that can help us to get the length of the string. So let us learn this with the help of the below code example.
C++ Program to find length of string function using length() function
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string str = "This is string length tutorial";
size_t strln = 0;
strln = str.length();
cout << "The length of the String is : " << strln << endl;
return 0;
}
Output
The length of the String is : 30
4. Use size Function to Find Length of a String in C++
The last method that we are going to learn is the size() function. The string class of the std library provides us a size() function that can help us to get the length of the string. So let us learn this with the help of the below code example.
C++ Program to find length of string using size() function
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string str = "This is string length tutorial";
size_t strln = 0;
strln = str.size();
cout << "The length of the String is : " << strln << endl;
return 0;
}
Output
The length of the String is : 30