+ 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!
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
+ 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){}