The string length in C++ can be calculated or found by various methods. Here, in this tutorial, we are going to learn all of them. And we would also implement the same through programming in C++.
Methods to find String Length in C++
The different methods following which we are going to calculate the length of a given string are listed below:
size()
length()
strlen()
- using loops.
The string.h header file in C++ comes with both the size()
and length()
functions pre-defined within itself. Often the two functions are used interchangeably and even perform in a similar manner returning the same values.
And on the other hand the cstring.h header file has strlen()
function pre-defined in it.
Interestingly, we can also find the length of a string in C++ by using a counter inside a loop structure. The loop iterations may be performed by either for
or while
.
Certainly, we cannot use the do-while
loop because of the fact that it iterates for a minimum of 1 time. So in a case where the string does not have any characters, the do-while loop would return 1 instead of 0.
So, let us see how we can all the above-mentioned methods to calculate the length of a string.
1. size() in C++
The size()
function in C++ returns the length of a specified set (String in our case) in terms of bytes. It is defined in the string.h header file in C. The code below depicts how the size()
function can be used:
#include<iostream> #include<string> using namespace std; int main() { string a; cout<<"Enter the string: "; getline(cin,a); cout<<"nThe size of the string is: "<<a.size(); return 0; }
Output:
2. length() to Find String length in C++
The length()
function is also defined inside the string.h header file and works in the same way size()
does. Both functions return the same values and are also used in the same way. Let us look at the example below:
#include<iostream> #include<string> using namespace std; int main() { string a; cout<<"Enter the string: "; getline(cin,a); cout<<"nThe size of the string usnig length() is: "<<a.length(); return 0; }
Output:

3. strlen() in C++
strlen()
is yet another function that is extensively used for calculating the length of a given cstring (Or an array of characters). It is defined in the cstring.h header file. Let us look at the example given below:
#include<iostream> #include<cstdlib> #include<cstring> using namespace std; int main() { char string[50]; cout<<"Enter the string: "; fgets(string,50,stdin); cout<<"nThe size of the string usnig strlen() is: "<<strlen(string)-1; return 0; }
Output:
As we can see, we get the length of the given string as our output. Since the fgets()
function also considers the newline character inside the string, the length of the string returned by the strlen()
function has to be decreased by 1.
4. Using loops to find string length in C++
The example below illustrates how using loop iterations we can count the length of a specified string.
#include<iostream> #include<cstdlib> #include<string> using namespace std; int main() { string a; int count=0; cout<<"Enter the string: "; getline(cin,a); while(a[count]!='') { count++; } cout<<"nThe size of the string using loop is: "<<count; return 0; }
Output:
The count variable is incremented by 1 on each successful iteration of the while loop until the end of the string or the NULL character(''
) is encountered. Consequently, we get the total number of elements present in the string. Which actually is the length of the string itself.
Conclusion
All the different methods used in this tutorial help us find string length in C++ where the size()
and length()
functions are used while handling C++ string objects. Whereas, strlen()
is applicable for C-type strings only. Loop iterations can be used for both cases.