reversing a string

Started by
21 comments, last by Nitage 18 years ago
Question goes like this How will you reverse a C-string without using any temporary variables? I tried this


#include <iostream>
#include<string>
int main()
{
	
char *str="dumbo";
std::cout<<strrev(str);
return 0;
}
But it didn't work but it works for str[]...I guess I understad the reason. because it's readonly. can't think of any other way without using temporary.
Advertisement
Do you want to actually reverse the string, or just print the characters in reverse order?

You can't reverse the string in your example, because str is pointing to read-only memory. Seriously.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
oh yeah...
That would just print in reverse order..I am still thinking of a way without using temporary variable.
For printing it backwards, I'd try this:

#include <iostream>#include <iterator>#include <algorithm>#include <cstring>int main(){    const char *str = "Hello World!";        std::reverse_copy(str, str+std::strlen(str), std::ostream_iterator<char>(std::cout));}


For reversing the string itself, I'd try this:

#include <iostream>#include <algorithm>#include <cstring>int main(){    char str[] = "Hello World!";        std::reverse(str, str+std::strlen(str));    std::cout << str;}


If for some reason using the STL isn't allowed, then I'm going to assume this is homework and I shouldn't be helping you.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Nice...they should work fine.
I'll just post my attempt of writing reverse function without temporary variable
maybe its just my bias, but i like my solution the best
void strrev(char *src){	char *ptr;		for (ptr = src; *ptr; ++ptr);	--ptr;		while (src < ptr)	{		*src ^= *ptr ^= *src ^= *ptr;		--ptr;		++src;	}}
_____________________#define ever (;;)for ever delete (void *) rand();
Why are you using arrays of characters?

std::string s = "blah";
std::reverse( s.begin(), s.end() );
Quote:Original post by Thetan
maybe its just my bias, but i like my solution the best


I hope I never have to maintain your code. [wink]

Seriously though, the standard library is your friend.
C++ all the way!

str.assign( str.rbegin(), str.rend() );
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
yet another STL trick:

string reversed = string(s.rbegin(),s.rend());

This topic is closed to new replies.

Advertisement