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 326
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
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185518674
Reply This Thread
up-rate
0
down-rate
Comments
hello sir, how can we solve this question...
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.

Sample Input - educated yielding dedication mediocre nationality

Sample Output - mediocre educated dedication nationality yielding
profile Divesh - anyforum.in Divesh
51  0  0
Posted On :2017-09-30 16:37:45.0
Leave a Comment
Hi Divesh,
Above solution is for the same problem. Please give it a try first and let me know if you face any problem.
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1855  18674
Posted On :2017-10-01 13:06:50.0
Leave a Comment



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