Reverse a Sentence
Here you will know that, How to reverse a sentence in java programming with example.
To make this program first we will get input a sentence (group of words) from the user, reverse the sentence using following method, and print the result.
For example:
Code Revise is my Website
etisbeW ym si esiveR edoC
Reverse a Sentence in Java Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.util.*; public class ReverseSentence { public static void main(String args[]) { String str, revStr = ""; Scanner in = new Scanner(System.in); System.out.print("Enter a string : "); str = in.nextLine(); for (int i = str.length() - 1; i >= 0; i--) { revStr = revStr + str.charAt(i); } System.out.println("Reverse Sentence is : "+revStr); } } |
Output
Check out our other Java Programming Examples