Active1 year, 4 months ago
- Java Program To Reverse Individual Words In A Sentence
- Java Program To Reverse The Words In A Sentence Pdf
- Java Program To Reverse Order Of Words In A Sentence
I have written a program to reverse the words in a string.if i/p is 'The dog is chasing'then o/p should be 'chasing is dog The'
But I don't know how to write this program using recursion. When I tried searching in stackoverflow, I could find reversing a string; but not reversing the words in a string.
Sethuraman Srinivasan
Sethuraman SrinivasanSethuraman SrinivasanContribute your code and comments through Disqus. Previous: Write a Java program to find the penultimate (next to last) word of a sentence. Next: Write a Java program that accepts three integer values and return true if one of them is 20 or more and less than the substractions of others. This is actually an interesting request. A 'sentence' in Java would normally be stored in a String object. However, you also requested an 'in place' sort, which is not possible to do with a String.
41111 gold badge77 silver badges1818 bronze badges
closed as too broad by John3136, Yvette Colomb♦, Andreas, Ian Stapleton Cordasco, Matt ClarkNov 18 '15 at 4:07
Reverse each word in a sentence [duplicate]. A few changes need to be done to you code: 1.change sentence.length() with words. Java Using a stack to reverse the. Java Program to reverse each word in String. We can reverse each word of a string by the help of reverse(), split() and substring() methods. By using reverse() method of StringBuilder class, we can reverse given string.
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
4 Answers
Recursive methods could seem a bit hard when beginning but try to do the following :
- Simplify the problem as much as possible to find yourself with the less complicated case to solve. (Here for example, you could use a sentence with two words).
- Begin with doing it on a paper, use Pseudocode to help you dealing with the problem with the simplest language possible.
- Begin to code and do not forget an escape to your recursion.
Solution
Yassin HajajYassin Hajaj15.5k88 gold badges3434 silver badges6868 bronze badges
Recursive method, using same logic as linked 'duplicate', without use of
split()
:The logic is:
- Take first char/word
- If that is last char/word, return with it
- Perform recursive call with remaining text (excluding word-separating space).
- Append space (if doing word)
- Append first char/word from step 1
- Return result
As you can see, when applied to reversing text (characters) instead of words, it's very similar:
For people who like things spelled out, and dislike the ternary operator, here are the long versions, with extra braces and support for null values:
Notice how the long version of
reverseText()
is exactly like the version in the linked duplicate.Community♦
AndreasAndreas88.7k44 gold badges7373 silver badges138138 bronze badges
This is a sample program which will do what you want recursively:
AtriAtri3,62744 gold badges2222 silver badges3737 bronze badges
ashisahuashisahu
Not the answer you're looking for? Browse other questions tagged javarecursion or ask your own question.
Active1 year, 8 months ago
Can anyone tell me how to write a Java program to reverse a given sentence?
For example, if the input is:
'This is an interview question'
Java Program To Reverse Individual Words In A Sentence
The output must be:
'question interview an is this'
Tot Zam4,42844 gold badges3535 silver badges5555 bronze badges
girigiri11.8k5959 gold badges125125 silver badges169169 bronze badges
14 Answers
You split the string by the space then iterate over it backwards to assemble the reversed sentence.
Tot Zam4,42844 gold badges3535 silver badges5555 bronze badges
OdedOded423k7878 gold badges776776 silver badges932932 bronze badges
(using
BozhoBozhoArrayUtils
and StringUtils
from commons-lang, but these are easy methods to write - just a few loops)504k113113 gold badges975975 silver badges10861086 bronze badges
Just being different: a recursive solution. Doesn't add any extra spaces.
I will also improve on the
polygenelubricantssplit
solution by using b
instead (it's so obvious!).Java Program To Reverse The Words In A Sentence Pdf
polygenelubricants296k106106 gold badges521521 silver badges599599 bronze badges
Bozho already gave a great Java-specific answer, but in the event you ever need to solve this problem without Java API methods:
To reverse, you can simply pop individual words onto a
stack
and pop them all back off when there are no words left.(Just to be extra clear, Java does provide a
PopsPopsStack
class, so it is possible to use this method in Java as well).19k3131 gold badges120120 silver badges148148 bronze badges
Just split it on a space character into a string array, then loop over the array in reverse order and construct the output string.
Rich AdamsRich Adams21.8k44 gold badges3131 silver badges5858 bronze badges
a every boring bit of java:
in groovy it's a little bit more readable:
Gareth DavisGareth Davis24.5k1111 gold badges6666 silver badges9999 bronze badges
I also give it a try: Here's a version using a stack and a scanner:
SearlesSearles
Bo Persson80.1k1717 gold badges128128 silver badges187187 bronze badges
gmhkgmhk7,9982323 gold badges7676 silver badges105105 bronze badges
nicer approach probably.. had seen the logic somewhere..here is my code which might do the job.
Java Program To Reverse Order Of Words In A Sentence
ShashiShashi
I don't think you should use any library.. 1) Reverse whole string 2) Reverse each word.
`
Rajender SainiRajender Saini47622 gold badges88 silver badges2222 bronze badges
No one has mentioned a vanilla Java 8 based solution yet, which is the same as Bozho's, but without any third-party libraries. So here it is:
Community♦
Lukas EderLukas Eder143k7878 gold badges467467 silver badges10231023 bronze badges
please try below solution, this is working for me.
Babasaheb MatsagarBabasaheb Matsagar
Before StringTokenizer was declared legacy, many used StringTokenizer for this. Thought I would just leave it here.
Sajal DuttaSajal Dutta
Rishabh AgarwalRishabh Agarwal
protected by KevJul 22 '12 at 13:57
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?