Skip to content

Instantly share code, notes, and snippets.

@eMahtab
Created December 12, 2015 16:24
Show Gist options
  • Save eMahtab/6402f4148475853ff954 to your computer and use it in GitHub Desktop.
Save eMahtab/6402f4148475853ff954 to your computer and use it in GitHub Desktop.
Reversing the order of words in String
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StringReversal2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String input=sc.nextLine();
List<String>words=new ArrayList<String>();
int position=-9;
while( (position=input.lastIndexOf(' '))!= -1 ){
words.add(input.substring(position+1));
System.out.print(words+" ");
input=input.substring(0,position);
System.out.println("Input :"+input);
}
words.add(input);
System.out.println(words);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment