Printing string capacity function in C++


This C++ code defines a string str and initializes it with the value "TutorJoe's is a Coaching centre". It then prints out the initial value of the string and its capacity using cout.

Here's a step-by-step breakdown of what happens in the code:

  • The iostream and string.h headers are included to allow input and output and to use strings respectively.
  • The using namespace std; statement is included to avoid having to use std:: before all standard library functions and types.
  • A string is defined, str, initialized with the value "TutorJoe's is a Coaching centre".
  • The initial value of str is printed to the console using cout.
  • The capacity() method is called on str, which returns the current capacity of the string. The capacity of a string is the amount of memory that has been allocated for it. In this case, it will depend on the implementation of the string class and the size of the string itself.
  • The value of the capacity of str is printed to the console using cout.
  • The main() function returns 0, indicating successful completion.

When the code is run, it should print out the initial string value and the capacity of the string to the console. The capacity of the string will depend on the implementation and the size of the string.

Source Code

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    string str = "TutorJoe's is a Coaching centre";
    cout<<"\nThe Initial string is : ";
    cout<<str<<endl;
    cout<<"\nThe capacity of string is : ";
    cout<<str.capacity()<<endl;
    return 0;
}
To download raw file Click Here

Output

The Initial string is : TutorJoe's is a Coaching centre

The capacity of string is : 31


Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples