9
brtln
5y

Learning C and just wrote a function to reverse chars in a char array (ex. "Hello" -> "olleH") but the array did not change.

It took me way to long to realize: I forgot to divide the length variable by two. So it reversed the array back everytime...

Comments
  • 1
    What were you dividing it by?
  • 1
    If you try to reverse strings like “tacocat” or “radar” it will always work.
  • 0
    @asgs by nothing :/
  • 1
    @bkwilliams haha true!
  • 0
    @brtln just curious. Why do you have to divide at all?
  • 1
    @asgs @brtln is probably doing something like:
    int len = sizeof(array) / sizeof(array[0]);
    len -= 1; // turns count into “max index”
    for (; len <= 0; len--)
    {
    // do stuff at the end and work forward
    }

    But that is going way back for me....
Add Comment