how to check the size of a string c++ code example

Example 1: how to get string length in c++

#include <iostream>
#include <string>

int main()
{
  string str = "iftee";
  
  //method 1: using length() function
  int len = str.length();
  cout << "The String Length: " << len << endl;
  
  //method 2: using size() function
  int len2 = str.size();
  cout << "The String Length: " << len2 << endl;
  
  return 0;
}

Example 2: c++ string size

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

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

Tags:

Cpp Example