4
string s; string adder;
for (int i = s.size ()-1; i >= 0; i--) {
    adder += s[i];
}
cout << adder << endl;

I am trying to reverse a string using c++ and am confused why do we have to do s.size()-1 and why does it print a space when we don't have -1?

PC Luddite
  • 5,883
  • 6
  • 23
  • 39

3 Answers3

4

Array indexes reside in [0, s.size()). s[s.size()] is one past the end of the array so you need to start at s.size()-1.

You can see that this is needed if you use the at() member function which uses bounds checking:

adder += s.at(i); // throws exception if i is out of bounds
David G
  • 94,763
  • 41
  • 167
  • 253
1

In you code s is a string variable that holds some characters. And as you said you want to display its contents in reverse order.

In order to do that, you have to navigate through the contents of s from back to front. To do that, as shown in your code you used for-loop.

Let's say s contains 10 characters. Since you are accessing your string s back-to-front, the last character found at the 10 - 1 = 9th index, because it starts counting from 0, not 1.

EDIT with Example

string original = "Hello";
string reverse;
for (int i = original.size() - 1; i >= 0; i--) {
    reverse += original[i];
}
cout << reverse << endl;

As you can see in the above example, original is a string variable which holds five characters, and reverse is also a string variable which is about to hold the contents of original in reverse order. In order to reverse the contents of original, we must navigate through it back-to-front.

original[4] = o
original[3] = l
original[2] = l
original[1] = e
original[0] = H

The above characters will be added one by one at each iteration of the for-loop as shown below:

reverse = "";  //it was empty first
reverse = reverse + original[4]; // now it holds the last character -> o
reverse = reverse + original[3]; // now it holds the characters -> ol
reverse = reverse + original[2]; // now it holds the characters -> oll
reverse = reverse + original[1]; // now it holds the characters -> olle
reverse = reverse + original[0]; // now it holds the characters -> olleh
rioki
  • 5,988
  • 5
  • 32
  • 55
  • While allocating an array let's say int array[5]. So this array can store 6 elements then? –  Nov 21 '18 at 19:04
  • @CyborgGamer No it stores 5 elements, and the indexes go from `0` to `4`. – David G Nov 21 '18 at 22:33
  • @CyborgGamer No it is not. The confusion will banish once you are more familiar with `arrays`. Array of size 5 will contains only 5 elements (array[0], array[1], array[2], array[3] and array[4]). –  Nov 22 '18 at 10:37
  • Hello guys, I am really rusty on arrays. Especially in C. How can I improve my knowledge on array? –  Nov 23 '18 at 11:04
  • Also, so lets if s.size is equal to 6 then it will contain six characters but the computer will count till zero right? –  Nov 23 '18 at 11:06
  • 1
    @CyborgGamer you should try to understand arrays from the scratch. Just ignore using your question as an example. If an array has size of 6, then it will always contain 6 items in it. But instead of accessing the last item using `array[6]`, we will use `array[5]`, because the counting starts from `array[0]` instead of `array[1]`. `array[0]` hold the first item, `array[1]` the second, ... and `array[5]` the sixth. It is simple as that. –  Dec 03 '18 at 05:58
0

It is because indexing is zero-based. So the first element is at position zero.

Since you use c++ you may (and should) use reverse iterators:

for (auto it = str.rbegin(); it != str.rend(); ++it) {
    char& c = *it;
    //do stuff
}

If you have boost you can just do:

for (auto c : boost::adaptors::reversed(str)) {
}
darune
  • 10,480
  • 2
  • 24
  • 62