From the course: Learning Java 11

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Solution: Find the bug in string reversal

Solution: Find the bug in string reversal - Java Tutorial

From the course: Learning Java 11

Solution: Find the bug in string reversal

- [Instructor] Let's find the bug in the string reversal implementation. Here we create a string builder and iterate through the original string backwards, adding a character to the string builder on each iteration. Then we turn the reverse string builder into a string. Let's change the show expected result variable to true and run the code to see what we expect, as well as what the code returns. (silence) It appears our reverse idea is working correctly, but we're forgetting a character. We're missing the H. Based on this, the bug is likely somewhere in our for loop. We're not looping over every element and adding it to our reverse string builder. Let's walk through how this code would work with a given data sample. Let's say our word was note. (silence) Walking through the implementation, I would start off at three, because the length of the string is four, and we subtract one. Then we append the character at…

Contents