How to calculate size of string in C++
What is string in C++?
In C++, a string is a sequence of characters. The string data type is part of the Standard Template Library (STL) and is defined in the <string> header. The std::string class provides a convenient and efficient way to manipulate strings in C++.
How to calculate size of string
There are some methods that helps us to calculate the size or we can say length of the string in C++ programming language.
Methods are as follows:
- Length()
- Strlen()
- Size
- Using loops
Using string::size
The length of a std::string object can be calculated using the .size() method of the std::string class.
The .size() method returns the number of characters stored in the std::string object, not including the null terminator.
Let’s understand with an example how we use string::size method in C++.
Example:
//program to calculate length or size of string using size method in C++
#include <iostream>
#include <string>
//main function of the program
int main() {
std::stringmyString = "Hello, My Name is Rohit Kumar";
int size = myString.size();
std::cout<< "The size of the string is: " << size <<std::endl;
return 0;
}
Output:
The size of the string is: 29
Explanation:
In this example, a std::string object named myString is declared and initialized with the string "Hello, My Name is Rohit Kumar". The .size() method is then used to calculate the size of the string and the result is stored in the size variable. Finally, the value of size is printed to the console using cout.
Note that the size() method returns an unsigned integer value of type std::string::size_type so you may need to cast the result to a signed integer type, such as int if you need to store it in a signed integer variable.
Using string::length
Basically in C++, we can also calculate the size of the string using the .length() method of the std::string class. The length() method also return the number of the character stored in a string, not including the null character.
It is an alias for the .size() method, and both methods produce the same result.
Let’s understand with an example how we use string::length method in C++.
Example:
// program to calculate length or size of string using length method
#include <iostream>
#include <string>
int main() {
std::stringmyDemo = "My name is Rohit Kumar and I am an intern in JTP";
int length = myDemo.length();
std::cout<< "The size of the string is: " << length <<std::endl;
return 0;
}
Output:
The size of the string is: 47
Explanation:
In this example, we named our string object as MyDemo and initialized it with the string “My name is Rohit Kumar and I am an intern in JTP”. With the help of the length() method, we calculated the size of the string and store the size of the string in the variable name size. Finally, the value of size is printed to the console using cout.
Using loops
You can calculate the size of a string using a loop in C++ by iterating over the characters in the string until you reach the null terminator (\0) character.
Here is an example using a C style string:
#include <iostream>
using namespace std;
int main() {
charmyString[] = "Good Morning My name is Akash Singh";
int size = 0;
for (inti = 0; myString[i] != '\0'; ++i) {
++size;
}
std::cout<< "The size of the string is: " << size <<std::endl;
return 0;
}
Output:
The size of the string is: 35
Explanation:
In this example, a for loop is used to iterate over the characters in the string myString. The loop continues until the current character is the null terminator (\0). For each iteration of the loop, the value of size is incremented by 1. When the loop terminates, the value of size is equal to the size of the string.
Some advantages of string methods
There are several advantages to using the .size() or .length() methods to calculate the size of a std::string object in C++:
- Convenience: The .size() or .length() methods provide a convenient way to get the length of a std::string object, without having to manually count the number of characters or use any additional functions.
- Accuracy: The .size() or .length() methods are accurate and reliable, and return the correct length of the std::string object, even if the string contains special characters or Unicode characters.
- Dynamic sizing: The std::string class is dynamically sized, so the length of a std::string object can change during the lifetime of the object. The .size() or .length() methods always return the current size of the std::string object, so you can use them to keep track of the size of the string even as it changes.
- Portability: The .size() or .length() methods are part of the std::string class, which is part of the Standard Template Library (STL), so they are portable and can be used with any C++ compiler that supports the STL.