0

I just needed to reverse a string, so I declared a new string variable and iteratively copied the elements. Now i want to print the reversed string through cout << reversed; but this is printing nothing. I can print it through a for loop through reverse[i] until the size but is there any better way?

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string original = "hello";
    string reverse;
    int i, j = 0, size = original.length();

    for (i = size - 1; i >= 0; i--) // original's last as reversed's first
    {
        reverse[j] = original[i]; 
        j++;
    }
    reverse[j] = '\0'; //last value as null
    cout << "original string = " << original << endl;
    cout << "reversed string = " << reverse  << endl;
    system("pause");
    return 0;
}
David G
  • 94,763
  • 41
  • 167
  • 253
Shoaib
  • 188
  • 2
  • 11

5 Answers5

5

A better solution is to use std::reverse:

std::string original = "whatever";
std::string rev = original;
std::reverse(rev.begin(), rev.end());
  • Although, as it has been also pointed out by others, constructing the copy using reverse iterators is an elegant solution. –  Feb 17 '14 at 14:56
2

You can use reverse iterators to instantiate the reversed string from the original:

string original = "hello";
string reverse(original.rbegin(), original.rend());

Then

std::cout << "reversed string = " << reverse << std::endl;

Note: avoid using namespace std;. There is an algorithm called std::reverse, whose name you could be inadvertently pulling into the global namespace. And you do have a variable with that name.

See this working demo.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • thanks, ok. But what if i use namespace std and declared reverse123, it would've been fine right? Just in this case it could cause problems? – Shoaib Feb 17 '14 at 14:55
  • 2
    @ShoaibHaider It would be, but you cannot know that in the future there won't be an `std::reverse123` in the standard library. Just avoid `using namespace std;`. – juanchopanza Feb 17 '14 at 14:57
  • @faranwath It is certainly more efficient than the one involving `std::reverse` :-) – juanchopanza Feb 17 '14 at 14:58
  • Ok my brothers and teachers, I am very thankful to all of you – Shoaib Feb 17 '14 at 15:00
  • @juanchopanza Certainly, but I thought `std::reverse` would be easier for him to understand. –  Feb 17 '14 at 15:00
  • @faranwath Yes, and it is a good algorithm to know in general. – juanchopanza Feb 17 '14 at 15:01
  • @ShoaibHaider : Read this why `using namespace std;` is not considered as good practice. http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Shravan40 Feb 17 '14 at 15:02
  • @juanchopanza If I could +2 your answer, I would ;) –  Feb 17 '14 at 15:02
1

The major problem with your code is that reverse is empty, so using any index leads to you indexing out of bounds and undefined behavior.

As a side-note, you don't have to terminate std::string objects, they are automatically terminated.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

you should notice that string reverse doesn't have a size , so when you do something like this

for (i = size - 1; i >= 0; i--) // original's last as reversed's first
{
    reverse[j] = original[i]; 
    j++;
}

Here, you are accessing an index that is not found in string reverse,as it's size is 0 . You should take care of something like that.

There are plenty of answers that your received about this question , I just wanted to make you notice this mistake

instead , you can make

reverse+=original[i];
amrdruid
  • 951
  • 13
  • 24
0

You don't even need to create a new string to hold the reversed string. Just iterator over the original string.

void print_reversed(const std::string& str)
{
    std::copy(str.crbegin(), str.crend(), std::ostream_iterator<char>(std::cout));
}
NickC
  • 385
  • 1
  • 10