String Class - end() iterator function

for(string::iterator it=s1.end();it!=s1.begin();it–)
{
cout<<(*it)<<endl;
}

If I write this code ‘m’ that is the first letter of string is not getting printed.
What changes are needed?

hi @official.shree31

#include <iostream>
using namespace std;
int main() {
    string s1 = "Hello World";
    for(string::iterator it=s1.end();it!=s1.begin();it--)
    {
    cout<<(*it);
    }
}

suppose this is the code then its o/p will be --> dlroW olle
there will be a space in beginning bcoz ur loop is starting from s1.end(), and it wont print the first character bcoz ur loop is going till it != s1.begin() where it skips the first character…
if u want to print the first character then run the loop till it!=s1.begin()-1
hope it clears ur doubt

hi @official.shree31
i hope its clear now??

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.