Reversing of words in java
You are given a phrase, you need to reverse the phrase.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class ReversingOfWords { | |
public static String reversingOfWords(String input1) | |
{ | |
String output=""; | |
String [] input=input1.split(" "); | |
int len=input.length-1; | |
output=input[len]; | |
for(int i=len-1;i>=0;i--){ | |
output+=" "+input[i]; | |
} | |
return output; | |
} | |
public static void main(String [] args){ | |
Scanner in = new Scanner(System.in); | |
String output; | |
String ip1 = in.nextLine().trim(); | |
output = reversingOfWords(ip1); | |
System.out.println(String.valueOf(output)); | |
in.close(); | |
} | |
} |
Input
dog bites man
Output
man bites dog
dog bites man
Output
man bites dog
Comments
Post a Comment