Created
December 12, 2015 16:24
-
-
Save eMahtab/6402f4148475853ff954 to your computer and use it in GitHub Desktop.
Reversing the order of words in String
This file contains hidden or 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.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