reversing a string object using the reverse function in algorithm lib – CSKC
Sign In

Search

Code Learning #CPP#3049    siteicon   siteicon  

reversing a string object using the reverse function in algorithm lib

Using the reverse() function directly along with begin() and end() iteration location setting functions of string class object

Learning Objectives

  • Learning to use reverse() function which is a library function for string objects.
  • The function needs start and end locations which are passed using the <object>.begin() and object.end() functions.

Source Code

TC++ #3049

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Code Understanding

#include <algorithm> //This header is required to use the reverse function

string st=”chocolates are tasty”; //Here we declare and initialise a test string

reverse(st.begin(),st.end());
Here we use the reverse function directly with parameters st.begin() and st.end() in order to tell the reverse function that how much part has to be reversed.

cout<<st<<endl;
The original string itself is reversed, so it can now be printed directly.

 


Share

Created: 26-Jan-2018 19:28 | Updated: 26-Jan-2018 19:32|