A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

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

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now