➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Reverse a Sentence In Java | Previously, we have seen Java programs to reverse a string and now in this post, we will develop a program to reverse a sentence in Java.
The reverse of a sentence means to reverse its index position. After reversing the sentence the first word of the sentence went into the last position and the last word of the sentence will go to the beginning of the sentence. Let us understand it through an example:-
Example-1:-
Sentence:- “Hi, How are you?”
The reverse of the sentence:- “you? are How Hi,”
Example-2:-
Sentence:- “I am learning Java programming language.”
Reversed sentence:- “language. programming Java learning am I”
Program to Reverse a Sentence In Java
Now let us see a simple program using loops to reverse a sentence in Java.
import java.util.Scanner;
public class Main {
public static String reverse(String sentence) {
StringBuilder reverse = new StringBuilder("");
String words[] = sentence.split("\\s");
for (int i = words.length - 1; i >= 0; i--) {
reverse.append(words[i]);
reverse.append(" ");
}
return reverse.toString();
}
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String sentence = scan.nextLine();
String reverse = reverse(sentence);
System.out.println("Reversed sentence: ");
System.out.println(reverse);
scan.close();
}
}
Output:-
Enter a sentence:
Welcome to Know Program.
Reversed sentence:
Program. Know to Welcome
In this program, we have defined a reverse() method which takes a string as the input value. We have declared a StringBuilder object to store the values. Later we split the original sentence based on the whitespace (“\\s”) and stored it in the string array.
Now, we have iterated the string array from last, fetched each word, and into the StringBuilder object. One whitespace is also added in each iteration. After iterating the string array, the StringBuilder object contains the reverse of the sentence.
Program to Reverse a Sentence In Java Using Recursion
We can do the same task using recursion techniques. Recursion is the technique where a method calls itself by dividing the problem into smaller components.
public static String reverse(String string) {
int i = string.indexOf(" ");
if (i == -1) {
return string;
}
return reverse(string.substring(i + 1))
+ " " + string.substring(0, i);
}
In this method, we are checking the index of the first occurrence of whitespace. If whitespace doesn’t exist in the string then it is a word, so directly return the string. Else call the recursive method by removing the first word in the sentence, for this, we have used the substring() method.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!