Java: Reverses the words in a string that have odd lengths
Java String: Exercise-109 with Solution
Write a Java program that reverses all odd-length words in a string.
Visual Presentation:
Sample Data:
(“Check two consecutive identical letters in a given string”) -> “kcehC owt evitucesnoc lacitnedi srettel in a nevig string”
("Create a Date object using the Calendar class") -> “Create a Date object gnisu eht Calendar ssalc”
Sample Solution:
Java Code:
public class Main {
public static void main(String[] args) {
// Define a string 'text' with a sentence
String text = "Check two consecutive identical letters in a given string";
System.out.println("Original text: " + text); // Display the original text
// Reverse the words with odd lengths in the string and display the modified string
System.out.println("\nReverses the words in the string that have odd lengths:\n" + test(text));
// Change the value of 'text' to another sentence
text = "Create a Date object using the Calendar class";
System.out.println("\nOriginal text: " + text); // Display the original text
// Reverse the words with odd lengths in the string and display the modified string
System.out.println("\nReverses the words in the string that have odd lengths:\n" + test(text));
}
// Method to reverse words with odd lengths in a string
public static String test(String str) {
// Split the string into words using space as a delimiter
String[] str_words = str.split(" ");
// Iterate through each word in the array
for (int i = 0; i < str_words.length; i++) {
// Check if the length of the word is odd
if (str_words[i].length() % 2 != 0) {
// Reverse the word using StringBuilder and update the array element
StringBuilder reverser = new StringBuilder(str_words[i]);
str_words[i] = reverser.reverse().toString();
}
}
// Join the modified words to form a string and return the result
return String.join(" ", str_words);
}
}
Sample Output:
Original text: Check two consecutive identical letters in a given string Reverses the words in the string that have odd lengths: kcehC owt evitucesnoc lacitnedi srettel in a nevig string Original text: Create a Date object using the Calendar class Reverses the words in the string that have odd lengths: Create a Date object gnisu eht Calendar ssalc
Flowchart:
Java Code Editor:
Improve this sample solution and post your code through Disqus
Previous Java Exercise: Check two consecutive, identical letters in a given string.
Next Java Exercise: Count duplicate characters in a String.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/string/java-string-exercise-109.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics