In this article, we will discuss different methods of finding the length of the string in C++. We install “DEVC++” software on our laptop to execute codes. First, we create a new file by tapping “Ctrl+N” from the keyboard. After coding, we compile and run the code by “F11” from the keyboard.
Use “While” and “For” Loop
The use of a while loop is just like a traditional method for finding the length of different strings. In using for and while loop, we adjust the variable “counter” to 0 and then add that counter from the beginning of the given string to the completion of the string (ends with a null character).
In this instance, we utilize two loops. A “for” loop and a “while” loop can determine the length of the definite string. First, we use preprocessor directives. It contains a header file. This is utilized at the start of the program. These directives start with the sign “#”:
using namespace std;
int main()
{
string str = "visual programming";
int i = 0,count=0;
while (str[i] != '\0')
{
++i;
}
cout <<"Length of the string by using While Loop: "<< i << endl;
for (i=0; str[i]!='\0'; i++)
{
count++;
}
cout <<"Length of the string by using For Loop: "<< count << endl;
return 0;
}
Here, we take #include<iostream> header file. Then, we use the main function. Every program in C++ contains the function main(), which is the first segment to be implemented when the code runs.
Now, we take the string “visual programming”. The variable used for this string is “str”. Further, we take two more variables: the “i” variable and the “count” variable. We declare the variable “i”. Here, we utilize a variable named “count” to determine the length of the string. We initialize both variables to zero. We use a while loop here. Each string terminates with “\0” and this is known as an escape sequence. This “\0” is not a distinctive character. It is an accurate number zero. The while loop executes until the variable “str[i]” is no longer equivalent to the escape series.
At the end of the loop, there is an addition in the value of “I” to 0 until the last element of the defined string is found. By this, we find out the length of a given string. We use “cout” to print the message “length of the string by using while loop”:
Now, we use the “for” loop. Here, the expression “i=0” initializes the variable “i” to 0. Initialization is done as soon as the loop is entered. This loop executes until the last character is reached. The expression “i++” increments the variable “i” each time the loop is executed. In the loop, the variable “count” is added each time until the termination of the defined string is reached. By this, we get the value of variable “count” and variable “i”. In the end, we again use “cout” to print the statement “length of the string by using for loop”.
Use Strlen() Function
“Cstring” is a library and it contains the function strlen(). In C++, we utilize strlen() function to get the string length. This is an in-built function. It is used in C-style strings. This built-in function returns the length of the defined string from the first character to the end null character:
#include <cstring>
using namespace std;
int main() {
char str[] = “I love to play badminto”";
int len = strlen(str);
cout <<“"Length of the string :“" << len << endl;
}
In this case, first, we use the “#include <iostream>” header file. And we must utilize the header file “#include <cstring>” at the beginning of the program to execute the code in which we use the strlen() function. The following code sample gets a C-style string and a char array and uses the strlen() function to get its length. We take a string “I love to play badminton” to get the length of this string.
The given string has 24 characters in it. So, we get the 24 output. We use “cout” to print the “length of the string” message.
Use Str.length() Method
Another method for finding the length of the given string is the use of the str.length() function. It provides the string length in bytes. It is the actual number of bytes corresponding to the characters of the string, not certainly its storing capacity. The object of the defined string grips the bytes without encrypting information that might be utilized to encrypt its characters. So, the return value might not reflect the real number of encrypted characters in the series of multi-byte characters:
#include <string>
int main ()
{
std::string str (“modern programming language”);
std::cout << “The length of the string is “ << str.length();
return 0;
}
We use two header files: “#include <iostream>” and “#include <string>”. We take the object “str” of the class “std::string”. Then, we want to get the string length for “modern programming language”. We use str.length() function. It is a built-in function. Another built-in function used to find out the length of the string is str.size(). Using both functions will return an identical result. These functions return the length of the given string in bytes:
For the strings of the class, we always utilize suitable methods. For example, we use str.length() or str.size() to find their lengths. The use of std::string is typically easier because it automatically allocates memory.
Conclusion
In this article, we explained several approaches that are used to get the length of different strings in C++. The C++ strings are arrangements of letters or characters saved in adjacent memory addresses. To obtain the length of the C-style strings, we utilize the strlen() method. In the string, the constructor sets it to a C-style string ending in “\ 0”. In the last method, we use a built-in function str.length(). This method is quite easy to implement because we just call the built-in function and obtain the length. We hope you found this article helpful. Check out other Linux Hint articles for more tips and information.