Hello, and welcome! Are you ready to elevate your string manipulation skills in Java? Today, we'll delve into a task that bolsters your comprehension of strings and enhances your creativity. The task involves splitting a string into words and then reversing each word as if reflected in a mirror. Does that sound interesting? Let's get started!
You're tasked with considering a string filled with words and writing a Java
function that accepts this string. The function should reverse the character order of each word and form a new string consisting of these reversed words.
Here's what you need to keep in mind:
- The input string will contain between 1 and 100 words.
- Each word in the string is a sequence of characters separated by whitespace.
- The characters can range from
a
toz
,A
toZ
,0
to9
, or even an underscore_
. - The provided string will neither start nor end with a space, and double spaces won't be present either.
- After reversing the words, your program should output a single string with the reversed words preserving their original order.
Example
Consider the input string "Hello neat java_lovers_123"
.
The function works as follows:
Hello
becomesolleH
neat
becomestaen
java_lovers_123
becomes321_srevol_avaj
Afterward, it forms a single string with these reversed words, producing "olleH taen 321_srevol_avaj"
.
Therefore, if you call reverseWords("Hello neat java_lovers_123")
, the function should return "olleH taen 321_srevol_avaj"
.
Let's begin breaking this down!
Our first task is to separate the words in the sentence. In Java
, the split()
method of the String
class allows us to achieve this easily. The delimiter you'll use in the split()
method is a regular expression for a single space " "
. Here is a sample code to illustrate this:
Java1String inputStr = "Hello neat java_lovers_123"; 2String[] words = inputStr.split(" "); 3 4// Now the array 'words' holds all the words of the string
Note that " "
as the regex delimiter ensures that the string is split at each space, effectively separating the words.
Next, we need to reverse each word separated in the previous step. In Java
, we can use the StringBuilder class, which provides a reverse()
method to do this. Let's add these lines to our existing code:
Java1String[] reversedWords = new String[words.length]; 2 3for (int i = 0; i < words.length; i++) { 4 StringBuilder sb = new StringBuilder(words[i]); 5 reversedWords[i] = sb.reverse().toString(); 6} 7 8// 'reversedWords' now contains the reversed words
Finally, we need to consolidate these reversed words into a single string, separated by spaces. We can achieve this using the StringBuilder class in Java
. Here's how we do that:
Java1StringBuilder result = new StringBuilder(); 2result.append(reversedWords[0]); 3 4for (int i = 1; i < reversedWords.length; i++) { 5 result.append(" ").append(reversedWords[i]); 6} 7 8String finalStr = result.toString();
First, we append the first word to the StringBuilder
, then append a space followed by the next word in a loop. This way, we add all words while preserving the format. The resulting string can be obtained by calling the toString()
method on the StringBuilder
.
It remains for us to combine the code from the steps together in a function reverseWords
and call it from the main
method to test.
Java1public class ReverseWords { 2 3 public static String reverseWords(String inputStr) { 4 String[] words = inputStr.split(" "); 5 String[] reversedWords = new String[words.length]; 6 7 for (int i = 0; i < words.length; i++) { 8 StringBuilder sb = new StringBuilder(words[i]); 9 reversedWords[i] = sb.reverse().toString(); 10 } 11 12 StringBuilder result = new StringBuilder(); 13 result.append(reversedWords[0]); 14 15 for (int i = 1; i < reversedWords.length; i++) { 16 result.append(" ").append(reversedWords[i]); 17 } 18 19 return result.toString(); 20 } 21 22 public static void main(String[] args) { 23 // Call the function 24 System.out.println(reverseWords("Hello neat java_lovers_123")); // prints: 'olleH taen 321_srevol_avaj' 25 } 26}
Well done! By completing this lesson, you've sharpened your proficiency in manipulating strings in Java
. You've improved especially in reversing the order of characters in a word. I hope you're feeling confident and excited about your Java
skills. Remember, mastering these skills requires frequent practice. Therefore, take some time to explore related problems and practice what you’ve learned. Enjoy the journey of learning!