C++

String Length in C++

In C++, while working with strings, we often need to find the length of the string. Therefore, determining the string’s length manually is exceedingly challenging. The string length refers to the size of the string that we created in the code. C++ provides numerous methods that aid in finding the length of the string in just one step. The methods for finding the string length in C++ programming make it easy for us to do this task.

Methods for Finding the String Length in C++ Programming

C++ facilitates us with five distinct methods that aid us in finding the length of the string more conveniently. These are as follows:

  • Utilizing the “length()” function
  • Utilizing the “size()” function
  • Utilizing the “strlen()” function
  • Utilizing the “for” loop
  • Utilizing the “while” loop

We will learn all these methods and the codes thoroughly in this article.

Example 1:

The code here begins with importing the “iostream” and “string” header files. The input/output stream library is integrated into C++ code using “iostream”. We may employ the strings and their related operators and the methods in our C++ programs using this “string” header file. The “namespace std” is placed underneath this and invokes the “main()” function here. Then, we initialize a variable named “vowels” with the “string” data type and assign “aeiou” to this “vowels” string.

Then, we utilize the “length()” method which aids in finding the length of the string that we previously added. With this “length()” function, we must type the string variable’s name whose length we want to render.

Code 1:

#include <iostream>
#include <string>
using namespace std;
int main() {
  string vowels = "aeiou";
  cout << "The length of the vowels string is: " << vowels.length();
  return 0;
}

Output:
The length of the given string that we added is now rendered here. We find this length using the “length()” method.

Example 2:

We import the header file and add the “std” namespace. After this, we have the driver code. Then, we initialize the “myFirstStr” string variable and assign some string data to this “myFirstStr” variable.

Now, we have to find the length of this string. For this, we utilize the “size()” method with this “myFirstStr” variable. We add this “size()” function with the string name inside the “cout”. This renders the size of the string on the console.

Code 2:

#include <iostream>
using namespace std;
int main() {
    string myFirstStr = "String Length Code!";
    cout << "The length of string is = " << myFirstStr.size();
    return 0;
}

Output:
This is now the rendered length of the string that we added in the code. The “size()” technique helps us determine the length of the string.

Example 3:

Two header files, “iostream” and “cstring”, are imported. Then, we type the “namespace std”. Underneath this, we call the “main()” method and initialize a character array with the string data. We place a variable named “myCharStr1[]” of the “char” data type and initialize it with the string data. Then, we have the “cout” in which we utilize the “strlen()” function and pass this variable in this function as the parameter. This “strlen()”, along with the variable name, is added inside the “cout”. So, it displays the length of this desired string.

Code 3:

#include
#include
using namespace std;
int main() {
    char myCharStr1[] = "Code for finding string length!";
    cout << "The length of the given string = " << strlen(myCharStr1);
    return 0;
}

Output:
This now displays the length of the string that we previously added. We utilize the “strlen()” function to determine this length.

Example 4:

After importing the two header files, “iostream” and “cstring”, we type the “namespace std.” The “main()” function is invoked and the string data is used to initialize a string variable. Here, we create a variable of the “string” data type called “str1” and assign the string data. Underneath this, we declare the “s” variable of the “int” data type and then utilize the “for” loop. We place this “for” loop to calculate the string’s length. We save the length of the string in the “s” variable and display this length with the aid of the “cout” and add “s” in this “cout”.

Code 4:

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    string str1 = "C++ Programming Codes";
    int s;
    for (s = 0; str1[s]; s++);
    cout <<  "We find the length of string using for loop which is " << s << endl;

    return 0;
}

Output:
The length is displayed here with the help of the “for” loop in our given code. We used the “for” loop to calculate the string’s length and rendered the string’s length here.

Example 5:

After including the header files and the “std” namespace, we then call the “main()” method in which we initialize the “newStringS1” as the string variable and assign some string to this variable. Underneath this, we have a variable named “a” of the “int” data type. Then, we utilize the “while()” loop. In this “while()” loop, we pass “newStringS1[a]” as the condition of this loop and then increment the value of the “a” variable. Now, it saves the length of the previously added string to this “a” variable. We render this length by utilizing the “cout” in which we add the “a” variable.

Code 5:

#include
#include
using namespace std;
int main()
{
    string newStringS1 = "String Length in C++";
    int a = 0;
    while (newStringS1[a])
        a++;
    cout << "The length of string using while loop is " << a << endl;
    return 0;
}

Output:
The “while” loop in our code helps to render the string’s length. The length is rendered in the following outcome:

Example 6:

Here, we invoke the “main()” method after including the header files and the “std” namespace. Then, we initialize the “myStrData ” string and allocate a string to it. In the next line, we have a variable of the “int” data type called “myStrLen” and initialize it with the “length()” method along with the string variable name. We place “myStrData.length()” to initialize the “myStrLen” variable. Now, the length of the given string is stored in this “myStrLen” variable. After this, we want to display this length. So, we add this “myStrLen” variable in the “cout”.

Code 6:

#include  
using namespace std;  
int main()  
{  
string myStrData = "Welcome to C++ Programming";  
int myStrLen = myStrData.length();  
cout<< "The length of the given string is : " << myStrLen;  
return 0;  
}

Output:
Now, the length of the string that we typed in the code is rendered in this outcome which we get with the help of the “length()” method.

Example 7:

Here, we import the “iostream” as well as the “cstring” header file. Then, the “std” namespace is added here. After this, the “main()” function is invoked. We create the character array by utilizing the “char” data type with the “s[]”array name. We initialize it with the string and use the “strlen()” method to find the length of this string. In this “strlen()” function, we pass “s” as the argument. Now, it returns the string’s length and renders it.

Code 7:

#include
#include
using namespace std;
int main() {
    char s[] = "kcuygwbwegghbkwlihweguuvwlbphuigefulwbcvwuwmen";
    cout << "The string length is "<< strlen(s) << endl;
    return 0;
}

Output:
The string length is “46” which is displayed in the following outcome. We get this length with the aid of the “strlen()” method.

Conclusion

The “string length” concept is explored here. We discussed five unique methods that help to find the length of the string in C++ programming. In this article, we explained all five methods in detail along with their practical demonstrations. We also showed how these methods return the length of the string in C++ programming.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.