Monday, May 24, 2010

In C++, how do you write a program that reverses a string?

For example, "hello" would return "olleh"

In C++, how do you write a program that reverses a string?
#include %26lt;string%26gt;


#include %26lt;iostream%26gt;


//...


char* orig = "hello";


std::string reverse;


for (int i=strlen(orig)-1;i%26gt;=0;i--) reverse += orig[i];


cout %26lt;%26lt; reverse;
Reply:push it onto a stack and pop it off





or push it on then set the stack pointer to the other end, then pop
Reply:Dude, DO YOUR OWN HOMEWORK! If you don't, you are not learning how to be a programmer.


No comments:

Post a Comment