Please help me out reversing the string using java code! | Sololearn: Learn to code for FREE!

+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!

10/21/2021 2:14:05 PM

Surbhi Yadav

6 Answers

New Answer

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

+3

You can use string builder or buffer for it Or iterate through entire loop backwards

+2

Can you tell us where you find difficulties in this? Kindly share your attempt with us! Surbhi Yadav

+2

Thanks for the answers! I got the code. https://code.sololearn.com/c4fqP5Xv9f3C/?ref=app

+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 ); } }

0

Show us what you've written so far. Here's a little help for ya 🙂🙂🙂🙂 StringReverse(String input){}