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
|
Run Output
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.
sunmitra| Created: 26-Jan-2018 | Updated: 26-Jan-2018|