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());
}
}
0
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
Hi Divesh,
Above solution is for the same problem. Please give it a try first and let me know if you face any problem.
It´s showing "program timed out.
Please check your code for infinite loop"
Hi Sushmitha,
It will show timeout if you do not provide console input as expected- input sentence should contain the words which start with the other's word trailing letter. Just write a sentence with same convention and press enter after executing the program.
Hi,
You can try this solution i just posted without while loop.