Table of Contents
Java program to reverse a string
A Java program to reverse a string.
Java program reverse a string using StringBuilder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.*; public class StringReverse { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print(" Enter any string : "); String msg = sc.nextLine(); StringBuilder sb = new StringBuilder(msg); sb.reverse(); System.out.println(" Reverse of "+msg+" is " + sb); } } |
Output

Java program reverse a string using reverse iteration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.*; public class StringReverse { public static void main(String[] args) { String rev=""; Scanner sc = new Scanner(System.in); System.out.print(" Enter any string : "); String msg = sc.nextLine(); char ch[]=msg.toCharArray(); for(int i=ch.length-1;i>=0;i--) { rev += ch[i]; } System.out.println(" Reverse of "+msg+" is " + rev); } } |
Output



PRACTICALS/PRACTICE PROGRAM IN Java
CHECKOUT OTHER RELATED TOPICS
We are aiming to explain all concepts of Java in easiest terms as possible.


ITVoyagers
Author