Java program to reverse a given string using the reverse method
Codeaft.java
importjava.util.Scanner;classCodeaft{publicstaticvoidmain(Stringargs[]){Strings;Scannersc=newScanner(System.in);System.out.println("———————————————————————————————————————————");System.out.println("Program to reverse the given string");System.out.println("———————————————————————————————————————————");System.out.print("Enter the string ");s=sc.nextLine();Stringr=newStringBuffer(s).reverse().toString();System.out.println("\nThe reverse string is "+r);System.out.println("———————————————————————————————————————————");}}
Output
codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
———————————————————————————————————————————
Program to reverse the given string
———————————————————————————————————————————
Enter the string Hello, World!
The reverse string is !dlroW ,olleH
———————————————————————————————————————————
codeaft@codeaft:~$
Dear User, Thank you for visitng Codeaft. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.