I can reverse it but can't figure out how to reverse it in pairs.

example - hello
output - ol le h

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

int main()
{
    string user;
    cout<<"Enter a random text"<<endl;
    getline(cin,user);


    for(int i = user.length();i>=0;i--)
    {
        cout<<user[i];
    }

    return 0;
}

Recommended Answers

All 3 Replies

Reverse the whole string( ex [hello] -> [olleh] ) then split the result into pairs( ex split pair of 2 [olleh] -> [ol le h]). If you are just outputting it, then output a space at every split position.

Reverse the whole string( ex [hello] -> [olleh] ) then split the result into pairs( ex split pair of 2 [olleh] -> [ol le h]). If you are just outputting it, then output a space at every split position.

hmmm how exactly would i do that...

I just need to know where to put this split at

in the cout statement the split doesn't seem to work there

and i can't seem to figure out how i can use it in the brackets of the loop

You can use a counter to count how many characters has been printed already and see if the counter is the split number. For example

int counter = 0;
int splitPoint  = 2; 

string str = "hello";
string rev = "olleh";

for each character in rev
   if startPoint == splitPoint print space and set startPoint to 0
   print current character rev[i]
   startPoint = startPoint + 1; 
endFor
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.