Java program to reverse a given string using an array
KW.java
importjava.util.Scanner;classKW{publicstaticvoidmain(Stringargs[]){Strings,r="";System.out.println("———————————————————————————————————————————");System.out.println("Program to reverse the given string");System.out.println("———————————————————————————————————————————");Scannersc=newScanner(System.in);System.out.print("Enter the string ");s=sc.nextLine();charsarr[]=s.toCharArray();intlen=0;for(charchr:sarr){len++;}System.out.println("\nThe length of a given string is "+len);for(inti=len-1;i>=0;i--){r=r+sarr[i];}System.out.println("\nThe everse string is "+r);System.out.println("———————————————————————————————————————————");}}
Output
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
———————————————————————————————————————————
Program to reverse the given string
———————————————————————————————————————————
Enter the string Hello, World!
The length of a given string is 13
The reverse string is !dlroW ,olleH
———————————————————————————————————————————
kodingwindow@kw:~$
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.