Java program to input a string from user and reverse each word of given string

This java program will read a string through input device and reverse each word in given string. Let suppose the input is "Hello Welcome in India" program will print the "olleH emocleW ni aidnI".

package com.includehelp.stringsample;

import java.util.Scanner;

/**
 * program to input a string from user and reverse each word of given string
 * @author includehelp
 */
public class ReverseEachWord {
    
    /**
     * Method to reverse each word in provided string
     * @param inputString
     * @return 
     */
    static String reverseWord(String inputString){
        String[] strarray = inputString.split(" ");  // Spilt String by Space
        StringBuilder sb = new StringBuilder();
        for(String s:strarray){
            if(!s.equals("")){
               StringBuilder strB = new StringBuilder(s);
               String rev = strB.reverse().toString();
               sb.append(rev+" "); 
            }    
        }
        return  sb.toString();
        
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enput String : ");
        String str = sc.nextLine();
        System.out.println("Input String : "+str);
        System.out.println("String with Reverese Word : "+reverseWord(str));
    }
    
}

Output

Enput String : Hello Welcome in India
Input String : Hello Welcome in India
String with Reverese Word : olleH emocleW ni aidnI 

Java String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.