Jump to content
  • Advertisement
Sign in to follow this  
swordance

making words backwards

This topic is 5414 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
someone just wrote a program on the lounge to do exactly that, the simplest way is with recursion, though it wouldn't be very hard to do with the standerd string libraries.

Share this post


Link to post
Share on other sites
you mean something like this?


void reverse( std::string& str )
{
const size_t str_size = str.size();
const size_t loops_required = str_size / 2;

for ( size_t i = 0; i < loops_required; ++i )
{
std::swap( str[ i ], str[ str_size - i - 1 ] );
}
}


Share this post


Link to post
Share on other sites
Uh, no.

void reverse_string(const std::string & src, std::string & dest)
{
dest = src;
std::reverse(dest.begin(), dest.end());
}

Share this post


Link to post
Share on other sites
Actually, if you are going to reverse copy, you should really use the correct algorithm so the semantics convey the proper intent:

void reverse_string(const std::string & src, std::string & dest)
{
dest.resize(src.length());
std::reverse_copy(src.begin(), src.end(), dest.begin());
}

Share this post


Link to post
Share on other sites

std::string s;
std::cin >> s;
std::copy( s.rbegin(), s.rend(),
//print it backwards to cout
std::ostream_iterator<std::string::value_type>( std::cout));
//alternative way
std::reverse_copy( s.begin(), s.end(), std::ostream_iterator<std::string::value_type>( std::cout));


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!