Java program to rotate each word in a string

Write a Java program to rotate each word in a String :

The problem is to rotate each word of a String. For this, we need to extract all words from the string. The following steps are used in this program to reverse each words :

  1. Get the string input by using the ‘Scanner’ class

  2. We will pass the string to ‘reverseWordsInString’ method . This method will return the updated string.

  3. First, split the string and store it in an array. Split is done on space or ” ”.

  4. Create one ‘StringBuilder’ object to store the result.

  5. Scan the array one by one word and reverse each words.

  6. To reverse a word: reverse the StringBuilder object and convert it to a string.

  7. Append the result String to the final stringbuilder.

  8. After all words are scanned, return the string by converting the string builder to a string object.

Program :

/*
 * Copyright (C) 2021 codevscolor.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Scanner;

/**
 * Class to reverse each words of a string
 */
public class ReverseStringWords {

    /**
     * System.out.println utility method
     *
     * @param value : value to print
     */
    static void print(String value) {
        System.out.println(value);
    }


    /**
     * Reverse the words in a string
     *
     * @param inputString : String to reverse the words
     * @return : Result String
     */
    static String reverseWordsInString(String inputString) {

        //first split the string by space
        String[] splitStringArray = inputString.split(" ");

        //create a stringbuilder object to store the result
        StringBuilder resultBuilder = new StringBuilder();

        //scan the string and rotate each words
        for (String word : splitStringArray) {
            if (!word.equals(" ")) {
                //only check for non-empty words

                //create one StringBuilder with the string word
                StringBuilder sb = new StringBuilder(word);

                //reverse the string builder and convert it to a string
                String reverseString = sb.reverse().toString();

                //append the string to the result string builder
                resultBuilder.append(reverseString + " ");
            }
        }

        //return the stringbuilder object
        return resultBuilder.toString();
    }


    /**
     * main method for this class
     */
    public static void main(String[] args) {

        //create a scanner class
        Scanner scanner = new Scanner(System.in);

        //string variable to save the user input string
        String userInputString;

        print("Enter a String : ");
        userInputString = scanner.nextLine();

        String result = reverseWordsInString(userInputString);

        print("Result String is "+result);
    }
}

Sample Output :

Enter a String :
The quick brown fox jumps over the lazy dog !!
Result String is ehT kciuq nworb xof spmuj revo eht yzal god !!

Similar tutorials :