Please help me out reversing the string using java code! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Please help me out reversing the string using java code!

The problem is as follows: The given code takes a string as input and converts it into a char array, which contains letters of the string as its elements. Sample Input: hello there Sample Output: ereht olleh Thanks!

21st Oct 2021, 2:14 PM
Surbhi Yadav
Surbhi Yadav - avatar
6 Answers
0
1st way class ReverseString { public static void main(String args[]) { String s=args[0]; StringBuffer sb=new StringBuffer(s); System.out.println(“The reverse of “ + s + “ is “ + sb.reverse()); } } In the above program String and StringBuffer both of them are classes Since String will not contain reverse method we are passing String parameter to StringBuffer and calling reverse method available in StringBuffer Way 2: class ReverseString { public static void main(String args[]) { String s=args[0]; System.out.println(“The reverse of “ + s + “ is “); for(int i=(s.length()-1);i>=0;i--) System.out.print(s.charAt(i)); } } In this method we are calculating length of a string Later on assigning strong length -1 to a variable named i Finally printing characters one by one using getChar method
22nd Oct 2021, 6:53 PM
sree harsha
sree harsha - avatar
+ 3
You can use string builder or buffer for it Or iterate through entire loop backwards
21st Oct 2021, 2:19 PM
Atul [Inactive]
+ 2
Can you tell us where you find difficulties in this? Kindly share your attempt with us! Surbhi Yadav
21st Oct 2021, 2:42 PM
Saurabh
Saurabh - avatar
+ 2
Thanks for the answers! I got the code. https://code.sololearn.com/c4fqP5Xv9f3C/?ref=app
21st Oct 2021, 4:13 PM
Surbhi Yadav
Surbhi Yadav - avatar
+ 1
import java.lang.*; import java.util.Scanner; class ReverseString { public static void main(String args[]) { Scanner scan = new Scanner(System.in); String s=scan.nextLine(); StringBuffer sb=new StringBuffer(s); sb.reverse(); System.out.println("\"" + s + "\" isminin tersten yazılışı : \n" + sb ); } }
23rd Oct 2021, 8:17 AM
Seçkin Karabel
0
Show us what you've written so far. Here's a little help for ya 🙂🙂🙂🙂 StringReverse(String input){}
21st Oct 2021, 3:31 PM
Dennis Torhov
Dennis Torhov - avatar