Java: Reverses the words in a string that have odd lengths
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:
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:
For more Practice: Solve these Related Problems:
- Write a Java program to reverse only the words that have an odd number of characters in a sentence.
- Write a Java program to process a string and invert the letters of each odd-length word.
- Write a Java program to split a sentence into words, reverse those with odd lengths, and reassemble the sentence.
- Write a Java program to iterate over words in a string and reverse the ones with an odd character count.
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics