How can you reverse a string?
Answers were Sorted based on User's Feedback
Answer / palemraju
String str="how r u";
int length=str.length();
System.out.print("The reverse String is:");
while(len>0)
{
System.out.print(str.charAt(len-1));
len--;
}
| Is This Answer Correct ? | 11 Yes | 2 No |
Answer / patil abhijeet
Use String buffer
StringBuffer stb = new StringBuffer(string)
stb.reverse()
| Is This Answer Correct ? | 7 Yes | 1 No |
Answer / milind s
Most of time in interview asked without using Build Function**
public String reverse(String arg)
{
String tmp = null;
if (arg.length() == 1)
{
return arg;
}
else
{
String lastChar = arg.substring(arg.length() - 1,
arg.length());
String remainingString = arg.substring(0,
arg.length() - 1);
tmp = lastChar + reverse(remainingString);
return tmp;
}
}
| Is This Answer Correct ? | 2 Yes | 0 No |
Answer / haneef
public class Main {
public static void main(String[] args)
{
String str1="HANEEF";
for(int i=str1.length();i>0;i--)
{
System.out.print(str1.charAt(i-1));
}
}
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / rajesh dangi
There is no reverse method in the String class for sure.
I haven't seen such method in any other class either. The
code snippet in response 1 is the best response for this
question.
| Is This Answer Correct ? | 2 Yes | 2 No |
Answer / venki from hyd
class ReverseString
{
static String name="Hello";
public static void main(String args[])
{
String rname;
for(int i=name.length(); i<0; i--)
{
rname=name.charAt(i);
}
System.out.println(rname);
}
}
| Is This Answer Correct ? | 1 Yes | 1 No |
Answer / gokulprasath
StringBuilder sb= new StringBuilder("kannada").reverse();
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / ravikiran(aptech mumbai)
by calling reverse() method from String class
| Is This Answer Correct ? | 2 Yes | 9 No |
Is ResultSet class?
5 Answers Bally Technologies, TCS,
How to restrict a member of a class from inheriting by its sub classes?
How to excute - Interface - Inner class- method can any one tell how to execute/ call this main method public interface abc { static int i=0; void dd(); class a1 { a1() { int j; System.out.println("inside"); }; public static void main(String a1[]) { System.out.println("in interfia"); } } }
What is the final variable?
Why do we need wrapper class?
What is the default size of set in java?
What is the final class modifier?
Can we define package statement after import statement in java?
What are the types of classes in java?
Difference between ?System.out.println? and ?System.error.println??
What is int lol?
What is 'finally' method in Exceptions?