1

I'm trying to figure out how this piece of code is executed step by step to better understand it. You type a word into the console and it spits out the word in reverse.

I can follow the code until I get to array[name.Length - i] = name[i - 1];

var array = new char[name.Length];
for (var i = name.Length; i > 0; i--)
    array[name.Length - i] = name[i - 1];     <------

I don't get how the steps of this one line of code is translated. Why do we subtract 1 from name.Length then subtract it by itself represented by i? What does subtracting 1 from i in name[i - 1] accomplish?

halfer
  • 19,824
  • 17
  • 99
  • 186
gusto
  • 13
  • 3
  • 3
    I'm not sure this code is even correct. Have you actually tried it yourself? – Tim Biegeleisen May 01 '18 at 05:04
  • Step through it with a debugger. Or, put the `for` loop in brackets `{}`. Then add `Console.WriteLine("array[{0}] = name[{1}]", name.Length - i, i - 1);` https://ideone.com/0QrOv2 – 001 May 01 '18 at 05:04
  • 2
    The code you have is incorrect. – Brian Driscoll May 01 '18 at 05:07
  • Why does everyone say the code is incorrect? I see nothing wrong with it! – 41686d6564 stands w. Palestine May 01 '18 at 05:26
  • @AhmedAbdelhameed They are not completely wrong. At the end of the loop the `array` will contains the chars in `name` in reversed order, but that's not the best way to reverse a string. Check out my answer, and more importantly, R. Matinho's answer I've linked to, to find out why. – Zohar Peled May 01 '18 at 05:28
  • @ZoharPeled I'm totally aware of that but I doubt that's what they meant. Saying that the code is incorrect to someone seeking help understanding how it works gives the impression that the code won't compile, will raise an exception, or something of that nature. It's just an exercise to practice the usage of arrays and loops after all. – 41686d6564 stands w. Palestine May 01 '18 at 05:32
  • @AhmedAbdelhameed I'm not going to assume I know what Tim or Brian thought about when posting their comments. I've shown in the rextester link in my answer that this code actually does what it's expected to do. As a side note, it's good you are aware of the accept problem, since a lot of developers I know are not aware of that. – Zohar Peled May 01 '18 at 05:36
  • @ZoharPeled It's actually a rare case to stumble onto if you don't use non-English languages, so that's totally understandable. My first language is Arabic so I think that helped somehow lol. – 41686d6564 stands w. Palestine May 01 '18 at 05:50
  • @AhmedAbdelhameed I totally agree. BTW, my native language is Hebrew and much like in Arabic we don't use accents but we do have our vowels as little dots and marks under or over the letters (though we usually don't bother writing them, except in children's books and poetry). I've only stumbled upon R. Martinho's answer by accident... – Zohar Peled May 01 '18 at 05:57

1 Answers1

3

The loop starts with i equals to the length of name, and subtract 1 from i in each iteration.

So in the first iteration, name.Length - i equals 0, while i-1 equals name.Length -1.
In the next iteration, name.Length - i equals 1, while i-1 equals name.Length -2, and so on.

By assigning the char at name[x] to array[y], the loop copies each char from the name array to the array array - so by the time the loop ends, the array array is exactly a reversed copy of the name array.

I've put together a small demo on rextester, so you can see it running.

Please note that this is not the best way to reverse a string, especially in a non-English language. Some languages use letters that are composed of more than one char, like accents in French, for example, that use Ç, é, â, ê, î, ô, û, à, è, ù, ë, ï and ü.

For these languanges, simply reversing char by char will result in a string where the accent is on the wrong letter. For a correct string reverse, check out R. Martinho Fernandes's answer to this SO post.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • 2
    This looks like the explanation I was looking for. Thank you for taking the time to explain the process to me and thank you to everyone else who replied or looked at my question! Greatly appreciated! (wow. the demo! makes it crystal clear. thanks again!!) – gusto May 01 '18 at 05:14
  • @gusto I've edited my answer with some more information (That might not matter to you right now but I think it's important to keep in mind and it might help other developers as well). You should read it again. Oh, and I'm glad to help :-) – Zohar Peled May 01 '18 at 05:26