C++ String Length


C++ String Length



”;


Length of a string is the number of characters present in the string. These characters can be of the data type char, and these include all alphanumeric elements, symbols and miscellaneous characters. In C++ programming language, there are two types of strings- Character Arrays of C-Style type, and String objects which are built-in objects of <string> class.

The length of a string also includes white spaces, but in case a string includes a terminating character “”, the string ends at that character and the count of length is terminated just before that character.

There are many ways to find the length of a given string. Some of these methods are iterative, whereas some also use in-built functions and methods. These methods are explained clearly in the following parts of this chapter −

  • Using strlen() Method
  • Using string::length() Method of String Class
  • Using string::size() Method of String Class
  • Using Iterative for Loop
  • Using Iterative while Loop

String Length Using strlen() Method

Strings are defined as character arrays which are accessed using the pointer to the first iterator of the array. We can use strlen() method of C Library to calculate the length of C-type arrays.

Syntax

The following syntax shows how to use strlen() method to calculate the length of the string −


strlen(string_name);

Example

The following example shows how to calculate the length of the string using strlen() method −


#include <bits/stdc++.h>
using namespace std;

int main() {
   char s[]="I love TP !!!";
   cout<<"Length of string s : "<<strlen(s);
   return 0;
}

Output


Length of string s : 13

String Length Using string::size() Method

Most programmers commonly use string::size() method of string class when there is a need to calculate the length of a string in C++ programming language. It is the most basic method, and it is generally used while traversing a string object.

Syntax

The following syntax shows how to use size() method to calculate the length of the string −


string_object.size();

Example

The following example shows how to calculate the length of the string using size() method −


#include <bits/stdc++.h>
using namespace std;

int main() {
   string s="I love TP !!! and others";

   cout<<"Length of string s : "<<s.size();
   return 0;
}

Output


Length of string s : 13

String Length Using string::length() Method

We can also use length() method of string class to determine the length of the given string. Both length() and size() methods are part of <string> header file, and these are called as methods to the string object.

Syntax

The following syntax shows how to use length() method to calculate the length of the string −


string_object.length();

Example

The following example shows how to calculate the length of the string using length() method −


#include <bits/stdc++.h>
using namespace std;

int main() {
   string s="I love TP !!! and others";

   cout<<"Length of string s : "<<s.length();
   return 0;
}

Output


Length of string s : 13

String Length Using while Loop

We can use a simple while loop to iterate over the string and initialize a variable count to calculate the length of the string until we reach the end of the string. For each iteration, the count increases by one, hence the net result will be the length of the string.

Syntax

The following syntax shows how to use a while loop to calculate the length of the string −


while(s[i]!='''') {
   [body]
}

Example

The following example shows how to calculate the length of the string using a single while loop −


#include <bits/stdc++.h>
using namespace std;

int main() {
   string s="I love TP !!! and others";
   int count=0, i=0;
   while(s[i]!='''') 
      count++, i++;

   cout<<"Length of string s : "<<count;
   return 0;
}

Output


Length of string s : 13

String Length Using a for Loop

We can use a simple for loop to iterate over the string and initialize a variable count to calculate the length of the string until we reach the end of the string. For each iteration, the count increases by one, hence the net result will be the length of the string.

Syntax

The following syntax shows how to use a for loop to calculate the length of the string −


for(int i=0;s[i]!='''';i++){
   [body]
}

Example

The following example shows how to calculate the length of the string using a single for loop −


#include <bits/stdc++.h>
using namespace std;

int main() {
   string s="I love TP !!! and others";
   int count=0;
   for(int i=0;s[i]!='''';i++) 
      count++;

   cout<<"Length of string s : "<<count;
   return 0;
}

Output


Length of string s : 13

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *