size of string in c++ code example

Example 1: length of string c++

// 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;
}

Example 2: c++ string size

// C++ string size
str.length();
str.size();			// synonym

Example 3: length of string in c++

str.length();

Example 4: 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 5: length of a string c++

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

Example 6: create a string of length c++

#include <string>
#include <iostream>

int main()
{
    std::string s(21, '*');

    std::cout << s << std::endl;

    return 0;
}

Tags:

Cpp Example