How to Find the Size of a String in C++?
You can find the size of the string in C++:
1: Find the Size of a String Using the std::string Functions
The std:: string has two functions size() and length() that allow you to find the size of strings in C++. Let’s find out how you can use these functions in C++.
i: Find the Size of a String Using size() Function
The size() function is a built-in function in C++ that allows users to find the size of strings in a program. To find the size of a string, you have to create an object with the string name followed by the size() function.
The syntax to use size() function in C++ is given below:
The following is the example code, in which we have defined the variable string str and to find the size of the str using size() function.
using namespace std;
int main() {
string str = "Welcome to Linux Hint";
cout << "Size of string is: " << str.size() << endl;
return 0;
}
ii: Find the Size of a String Using length() Function
You can also use the length() function to find the size of a string in C++. The length() function is used with the character, words, or paragraphs. In C++, the length and size functions provide the same values.
The general syntax of the length() function used in C++ is:
Below is the example code for using the length() function for finding the size of the string:
using namespace std;
int main() {
string mystring = "Welcome to Linux Hint";
cout << "Size of string is: " << mystring.length() << endl;
return 0;
}
2: Find the Size of a String Using strlen() Function
The strlen() is another built-in function in C++ that returns the number of characters passed through the string. This is function is present under the cstring header file and can also be utilized for finding the size of a string in C++.
The general syntax to use this function is:
The following example illustrates the use of strlen() function in C++ for finding the string size:
#include <cstring>
using namespace std;
int main() {
char str[] = "Welcome to Linux Hint";
cout << "Length of the string is "<< strlen(str) << endl;
return 0;
}
3: Find the Size of a String Using Loops
Another way of finding the size of the string in C++ is via the loops. The two loops structures that can be used are:
i: Using for Loop
The for loop can be used to find the string size and for that purpose you first need to initialize the counter variable that will count the string size. This counter will be incremented by the one at each iteration. The loop breaks when the counter reaches the null character.
The following example shows the complete process of for loop in C++ for finding the string size:
using namespace std;
int main() {
int x, count = 0;
string str = "Welcome to Linux Hint";
for (x = 0; str[x]; x++) {
count++;
}
cout << "The size of string is: " << count << endl;
return 0;
}
ii: Using while Loop
The method of finding the length of the string with the while loop is the same as the for a loop, which requires initializing the count variable and then incrementing the counter at the execution of the while loop:
Here is an example of using the while loop in C++ to find the size of a string.
using namespace std;
int main() {
int j = 0, count = 0;
string str = "Welcome to Linux Hint";
while (str[j]) {
count++;
j++;
}
cout << "The size of string is: " << count << endl;
return 0;
}
Conclusion
In C++ programming, a string is a sequential collection of characters, and a string’s length is essential while coding with them. Multiple functions can be used to find a string size in C++ including size(), length(), and strlen(). You can also use for or while loop to find the size of a string in C++. The complete detail is provided in the above-mentioned guidelines.