Resize string in C++


This is a C++ program that demonstrates the use of the resize method in the std::string class. The resize method is used to change the size of a string to a specified length, either by truncating the string or by adding additional characters. The program works as follows:

  • The input string str is defined and initialized with the value "TutorJoe's is a Coaching centre".
  • The initial value of the string is printed to the console using the cout object.
  • The resize method is called on the string str, with an argument of 10. This resizes the string to a length of 10 characters, either by truncating the string or by adding null characters at the end.
  • The resulting value of the string is printed to the console using the cout object.
  • The program exits with a return value of 0.

Overall, this program demonstrates how to use the resize method to change the size of a string in C++, either by truncating the string or by adding additional characters.

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;
    str.resize(10);
    cout<<"\nThe string after resize is : ";
    cout<<str<<endl;
    return 0;
}
To download raw file Click Here

Output

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

The string after resize is : TutorJoe's


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