0

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;
}
2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by firstPerson
0

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.

0

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

Edited by myrongainz: n/a

0

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

Edited by firstPerson: n/a

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.