A C++ string is a sequence or collection of characters.
Length or size of a string means the total number of characters that are present in it.
This article will explain different ways to find the length of a string in C++ with programs and examples.
In C++, a string can be declared in two ways:
- As a character array such as
char[] str = "Hello";
- As an object of string class such as
string str = "Hello";
First representation is also known as C-style string or simply C-string since this syntax is also used to represent a string in C.
Method 1: Using strlen() function
strlen()
function is used to find the length of a C-string. It accepts a character array as argument and returns its size.
This function is defined in cstring header file so you should include it when using this method.
Example,
#include <iostream> #include <cstring> using namespace std; int main() { char s[30]="Hello! Welcome to C++"; cout << "Length of string is: " << strlen(s) << endl; }
This outputs
Length of string is: 21
If you want to get the length of a string declared in C++ style, that is, using string, with strlen()
function, then it needs to be converted to a C-style string by using c_str()
function as shown below.
#include <iostream> #include <cstring> #include <string> using namespace std; int main() { string s = "Hello! Welcome to C++"; cout << "Length of string is: " << strlen(s.c_str()) << endl; }
Remember that c_str()
function is defined in string header file, so this needs to included as well.
String class has a built-in member function
size()
which returns the number of characters in a string. This function does not take any arguments.Example,
#include <iostream> #include <string> using namespace std; int main() { string s = "Hello! Welcome to C++"; cout << "Length of string is: " << s.size() << endl; }
which prints
Length of string is: 21
Method 3: Using length() function
String class contains a length()
function which also returns the number of characters in a string. Example,
#include <iostream> #include <string> using namespace std; int main() { string s = "Hello! Welcome to C++"; cout << "Length of string is: " << s.size() << endl; }
Output is
Length of string is: 21
String length() vs size()
Both length()
and size()
functions return the same result, then a question arises why two different methods in the same class.
Reason is that size()
is defined for consistency with data structures such as map, vector etc., while length()
is to improve readability since the word length is best suited with strings.
Both these methods are same and have same complexity.
In this method, a for loop is used to iterate over the characters of string. An integer variable is incremented by 1 in every loop iteration.
This variable is initialized to 0 before the start of loop. After the loop ends, it will be equal to the number of characters in the string or its length.
Example,
#include <iostream> #include <string> using namespace std; int main() { string s = "Hello! Welcome to C++"; // initialize counter int count = 0; // loop over string for (int i = 0; s[i];i++) { // increment variable count++; } cout << "Length of string is: " << count << endl; }
Output of this program is
Length of string is: 21
Method 5: Using while loop
This method is the same as the previous one but it uses a while loop to iterate over the string.
#include <iostream> #include <string> using namespace std; int main() { string s = "Hello! Welcome to C++"; // initialize counter int count = 0; // loop over string for (int i = 0; s[i];i++) { count++; } cout << "Length of string is: " << count << endl; }
Output of this program is
Length of string is: 21
An iterator is used to loop over a string. A variable is initialized to 0 and incremented in every iteration.
When the iterator completes traversing the string, the variable value will be equal to the number of characters in the string. Example,
#include <iostream> #include <cstring> using namespace std; int main() { string s = "Hello! Welcome to C++"; // declare iterator string::iterator it; int count = 0; // loop with iterator for(it = s.begin(); it!=s.end(); it++){ count++; } cout << "Length of string is: " << count << endl; }
In this article, all the methods except the first find the length of a string without using strlen()
function.