Check out example codes for "length of a string c++". It will help you in understanding the concepts better.

Code Example 1

string str ="hello world"; 
//different ways to find length of a string: 
str.length(); 
str.size();

Code Example 2

string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt 
  string is: " << txt.length();

Code Example 3

// string::length
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  std::cout << "The size of str is " << str.length() << " bytes.\n";
  return 0;
}

Code Example 4

string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt 
  string is: " << txt.size();

Code Example 5

str.length();

Code Example 6

#include<iostream>
#include<cstring>
using namespace std;
main() {
   string myStr = "This is a sample string";
   char myStrChar[] = "This is a sample string";
   cout << "String length using string::length() function: " << myStr.length() <<endl;
   cout << "String length using string::size() function: " << myStr.size() <<endl;
   cout << "String length using strlen() function for c like string: " << strlen(myStrChar) <<endl;
   cout << "String length using while loop: ";
   char *ch = myStrChar;
   int count = 0;
   while(*ch != '\0'){
      count++;
      ch++;
   }
   cout << count << endl;
   cout << "String length using for loop: ";
   count = 0;
   for(int i = 0; myStrChar[i] != '\0'; i++){
      count++;
   }
   cout << count;
}

Learn ReactJs, React Native from akashmittal.com