课程: Java Algorithms

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Solution: Reverse each word

Solution: Reverse each word - Java教程

课程: Java Algorithms

Solution: Reverse each word

- [Instructor] Let's create an algorithm that reverses each word in a sentence. The first step is to check if the input falls into a base case category. This is where the sentence is null or the sentence is empty. If that's the case, we'll return the empty string. Now we're ready to implement the core of the function. Let's create a result variable to hold our end result. This'll be a StringBuilder because we'll slowly build up our result with each reversed word before we return it from the function. For this challenge, we're reversing each individual word, not the entire input string itself. In order to reverse, we'll need to pluck out each word so we can individually reverse it. We can use the split function on the input to create an array of words. Each word is separated by spaces. The split function separates the input string into separate strings, splitting on the space, which is the delimiter character we've added…

内容