C++

How to Find the Size of a String in C++?

In C++, strings are a group of characters that are double-quoted. They are represented as the array of characters and the null character at the string’s end. It is important to find the size of the string while dealing with them in the code. C++ provides different methods for identifying the length of the string, we will discuss them in detail in the next section of the guide.

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:

string_name.size();

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.

#include <iostream>

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:

string_name.length();

Below is the example code for using the length() function for finding the size of the string:

#include <iostream>

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:

strlen(string_name);

The following example illustrates the use of strlen() function in C++ for finding the string size:

#include <iostream>

#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:

#include<iostream>

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.

#include<iostream>

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.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.