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: Arrange the words as letter chain

Given a sentence, write a program to arrange and print the words in the sentence such that the last letter of the previous word is same as the first letter of the next word. Assume the first word is correctly placed in order and start arranging the others. Sample Input - mediocre yielding dedication nationality educated Sample Output - mediocre educated dedication nationality yielding

corejava x 323
string-handling x 34
Posted On : 2017-07-06 20:38:00.0
profile keerthana keerthu - anyforum.in keerthana keerthu
300
up-rate
3
down-rate

Answers


import java.util.Scanner;

public class ReorderSentence {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String sentence=input.nextLine();
String[] words=sentence.split(" ");
StringBuilder formattedString=new StringBuilder(words[0]);
while(formattedString.toString().length()!=sentence.length()){
for(int i=1;i<words.length;i++){
if(formattedString.toString().endsWith(Character.toString(words[i].charAt(0)))){
formattedString.append(" "+words[i]);
}
}
}
System.out.println(formattedString.toString());
}
}

Posted On : 2017-07-06 21:56:39
Are You Satisfied : 1 Yes  0 NO
profile Rishi Kumar - anyforum.in Rishi Kumar
523184717358
Reply This Thread
up-rate
0
down-rate



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