How to reverse a string

I have the following codes:

...
...
string line("this is a test");
...
...

how can I reverse this string line?

P.S.:
No c-function such as strrev().
chenweiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HoegjeCommented:
Homework maybe ?

How do you do something like that ? Think a bit for yourself. Take small steps at a time.. There are several ways to do this, so pick any of them..
0
rstaveleyCommented:
You might consider reverse iterators (rbegin() and rend()). Enough said, assuming this is HW.
0
kohashiCommented:
This sounds like homework so I will provide psuedo code to do it an easy fashion:

get initial string length
create a string of the same length
run a loop that counts down from length to zero
inside the loop place the original string[x] (assuming x is the decrementing variable) inside of newstring[length-x]

once the loop completes, you will have your string in reverse.  You may need to make it length-x-1 or add one and other such minute changes to make sure you dont exceed the allotted space and it gets put in the proper place.

hope that helps,
-kohashi
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

mnashadkaCommented:
There's also a reverse function in STL.  This will work on string iterators.
0
skp23Commented:
You don't really need the new string.
You can simply swap the first and last, then the next pair and so on.
0
zania22Commented:
here is the code;

string line("this is a test");

//get the length of the string
int length = line.length();

//just print out backwards with a for loop
int i;
for(i=length-1;i>=0;i--){
    cout<<line[i];
}

cout<<endl;

good luck!
-zania22

0
rstaveleyCommented:
zania22, you just did what Hoegje, rstaveley, kohashi, mnashadka, skp23 and many other EE members, who refrained from commenting, carefully avoided doing. Woops.
0
t_chinhoeCommented:
May be chenwei can use other methods doing it... like recursive
0
xssassCommented:
the loop of zania22 is all right, but if it is a C++ style String, it starts counting from 1.

So it should be:

#include <string.h>
#include <iostream.h>

String line="This is a testline";

for (int i=line.Length(); i>=1; i--){
  cout << line[i];
}
cout << endl;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chenweiAuthor Commented:
Thanks for the info from all sites. But truely to say, the method you gave me is not bad but is a very normal one which can be found out everywhere.

Originally what I want is to look for a simple function such as strrev() in C.

0
rstaveleyCommented:
Here's the reverse iterators:
--------8<--------
#include <iostream>
#include <string>

int main()
{
      std::string test("this is a test");
      std::string reversed_test(test.rbegin(),test.rend());
      std::cout << reversed_test << '\n';
}
--------8<--------
I don't expect that it will score you points in a homework assignment, though. They's be more likely to be looking for loops etc.
0
mnashadkaCommented:
reverse is found in <algorithm>
0
rstaveleyCommented:
> They's

They'd

[Nearly mis-typed that the same way in my correction too. Must push my keyboard to the left a bit.]
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.