How to arrange the word as per alphabetical order based on the first letter of the word in the sentence? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to arrange the word as per alphabetical order based on the first letter of the word in the sentence?

Without using compareTo() function and swapping using two loop. Need simple way rather than complex.

10th Jun 2020, 1:24 PM
shubham kumar
shubham kumar - avatar
7 Answers
+ 1
Done successfully by me
11th Jun 2020, 10:12 AM
shubham kumar
shubham kumar - avatar
0
Hello shubham kumar You can get the first letter of a string with .charAt(0). 0 is the first index. String s = "hello"; char firstChar = s.charAt(0); If you have two words you can use an if statement: if(word1.charAt(0) > word2.charAt(0){ //swap words } So your task is like sorting a char array. I suggest you to have look into the bubble sort algorithm. https://www.sololearn.com/learn/649/?ref=app
10th Jun 2020, 2:51 PM
Denise Roßberg
Denise Roßberg - avatar
0
Hello Denise RoBberg, I tried this way but Again the trouble is after getting word of sentence in an array form using split (" ") function generates difficulty to set for loop and execute character extraction of that word and analyse and set that word as per that character in ordered sequence.If you help me through a code,I will be thankful to you.I tried but it failed.
10th Jun 2020, 3:06 PM
shubham kumar
shubham kumar - avatar
0
Hello shubham kumar Can you show me what you have tried so far?
11th Jun 2020, 9:45 AM
Denise Roßberg
Denise Roßberg - avatar
11th Jun 2020, 10:12 AM
shubham kumar
shubham kumar - avatar
0
Do you still need help?
11th Jun 2020, 10:15 AM
Denise Roßberg
Denise Roßberg - avatar
23rd Jul 2021, 2:27 AM
Aaron Bhatta
Aaron Bhatta - avatar