Reverse Each Word Of An String | Java Programs

Output:

*** Reverse Each Word Of String ***

Enter String: My name is alee

Original String:        My name is alee
Reversing String Words: yM eman si eela 

Click Here For Java Online Compiler

Solution:

import java.util.Scanner;

public class ReverseEachWord {

    static void reverseEachWord(String string) {
        String[] words = string.split(" ");

        String reverseString = "";
        int length = words.length;
        for (int i = 0; i < length; i++) {
            String word = words[i];

            String reverseWord = "";

            for (int j = word.length() - 1; j >= 0; j--) {
                reverseWord = reverseWord + word.charAt(j);
            }

            reverseString = reverseString + reverseWord + " ";
        }

        System.out.println("\nOriginal String:        " + string);
        System.out.println("Reversing String Words: " + reverseString);
    }

    public static void main(String[] args) {
        System.out.println("*** Reverse Each Word Of String ***" + "\n");
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter String: ");

        reverseEachWord(scanner.nextLine());
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon