Reversing string

Is there a command, so that i can read strgin reversed or is there some way to reverse the string.
What do you mean? Basically output a message
string text;
cout<<"Enter a text:";
cin>>text;
when someone or you enter a text it reverse the element of the text so you can read the text backward instead of forwards.
If so Yes there is a way you can do that and it easy.
I don't think there is a function for this so you probably have to make one. Using
 
char text[number_here]

is easier. You just use a loop, that starts from the end of the array and ends when it becomes 0 (zero) and you copy the elements from text to text_reverse or any other variable.
welll yeah. But i dont know how long the array will be. Amille, what is that way?
1
2
3
4
5
6
use this code to reverse string
use string header file and write.

cout<<"Enter your text"<<endl;
cin>>b;
cout<<endl<<strrev(b);
So, i can use this strrev with strok? I need to reverse the string and then cut it into pieces, 1 character is 1 substring
Last edited on
Do you need string or c_string? If string

1
2
3
4
5
6
7
8
9
10
string reverse_string(string line) 
{
	string::reverse_iterator it;
	string result;
	for (it = line.rbegin(); it < line.rend(); it++) 
	{
		result += *it;
	}
	return result;
}
There is standard algorithm std::reverse that does what you need

std::reverse( s.begin(), s.end() );

There is also a simple method to define another string which will be equal to the reversed original string. For example

1
2
3
4
5
std::string source;

std::cin >> source;

std::string target( source.rbegin(), source.rend() );
so if source is tere then target is eret?
so if source is tere then target is eret?

That is right.
std::string siht(alge.rbegin(), siht.rend());
This part is causing errors. Whenever it is time to reverse the string, it just crashes.
Last edited on
It is not this statement that causes the error.
Yes, it is i just noticed :D std::string siht(alge.rbegin(), siht.rend());
If you look carefully, you see that i have alge.rbegin() but siht.rend()
I did not take this into account because it might be a typo.
That was the problem. Now its working. Ty for your help.
Topic archived. No new replies allowed.