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 353
string-handling x 34
Posted On : 2017-07-06 20:38:00.0
profile keerthana keerthu - anyforum.in keerthana keerthu
200
up-rate
2
down-rate

Answers


Better and safe approach is instead of while loop just use for loop and it´ll give the output at least one word if there is no word starts with the trailing letter of first word.

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]);
for(int j=1;j<words.length;j++){
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 : 2018-06-05 19:52:36
Satisfied : 3 Yes  2 No
profile Garima Gupta - anyforum.in Garima Gupta
596129026817
Reply This Thread
up-rate
5
down-rate

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  17 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188222642
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
101  18  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  1882  22642
Posted On :2017-10-01 13:06:50.0
Leave a Comment
It´s showing "program timed out.
Please check your code for infinite loop"
profile Sushmitha Gaddam - anyforum.in Sushmitha Gaddam
0  0  0
Posted On :2018-06-05 16:24:53.0
Leave a Comment
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.
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1882  22642
Posted On :2018-06-05 19:34:06.0
Leave a Comment
Hi,
You can try this solution i just posted without while loop.
profile Garima Gupta - anyforum.in Garima Gupta
596  1290  26817
Posted On :2018-06-05 20:16:34.0
Leave a Comment



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