w3resource

Java: Reverses the words in a string that have odd lengths


109. Reverse Odd-Length Words

Write a Java program that reverses all odd-length words in a string.

Visual Presentation:

Java String Exercises: Reverses the words in a string that have odd lengths


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:

Flowchart: Java String Exercises - Reverses the words in a string that have odd lengths



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.

Go to:


PREV : Check Consecutive Identical Letters.
NEXT : Count Duplicates Occurring More Than Twice.

Java Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.