AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-string-handling: String split

Write a java program that takes a line of text as input, which consists of several words separated by "+" and prints the words in input text in reverse order.
Sample Input : This+is+a+very+simple+sentence
Sample Output : sentence simple very a is this

corejava x 353
string-handling x 34
Posted On : 2016-11-15 12:22:44.0
profile Boopalan - anyforum.in Boopalan
3730
up-rate
2
down-rate

Answers


import java.util.Scanner;

public class ReverseSentence {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String[] string=input.nextLine().split("\\+");
for(int i=string.length-1;i>=0;i--){
System.out.print(string[i]+" ");
}
}
}

Note:
---------------------------
split(...) can´t work simply like split("+") as + is an concatenation operator also. So we have to use regular expression in split function.

Posted On : 2016-11-16 00:40:05
Satisfied : 1 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129026819
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in