Short and Spicy question is that If input is how are you
and output should beyou are how
.
public class ReverseWord {
public static void main(String[] args) throws IOException{
String input;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the input string");
input = br.readLine();
System.out.println("Enterd String:"+input);
System.out.println("Reversed:"+reverse(input));
}
public static String reverse(String input){
String delimiter = " ";
String[] splits = input.split(delimiter);
String reverse = new String("");
for(int i=splits.length-1; i>=0; i--){
//System.out.println(splits[i]);
reverse += splits[i];
reverse = reverse.concat(delimiter);
}
System.out.println(reverse);
return reverse;
}
}
How can I improve my code? And Is there any other way to do it with less time and space complexity?
reverse
should behave if input isn'thow are you
? \$\endgroup\$