Im working on this program: to write a program that displays the last 10 characters in an order that is reverse of the order they were entered... i've gotten this so far:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string message;

    cout << " Enter a string that is longer than 20 characters:\n " << endl;
    getline(cin, message);
    cout << endl;
    cout << " The string backwards is:\n " << endl;
    cout << endl;

    string::reverse_iterator rit = message.rbegin();
    while (rit != message.rend())
    {
        cout << *rit++;
    }
    cout << endl;
}

this just displays the the message backwards...what do i need to do to make it display that last 10 characters of the orginal message backwards.. can anyone help?

you can simply add a counter to the loop that stops when you've displayed the number of characters you need:

string::reverse_iterator rit = message.rbegin();
int counter = 0;

while (counter < 10 && rit != message.rend())
{
   cout << *rit++;
   counter++;
}
cout << endl;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.