0

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?

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by vmanes
0

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;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.