Go to Top

Saturday, 1 October 2011

To reverse a String

Question 48 : Write a program inn Java to reverse a String taken as input in Java.

Sample input : JAVA

Sample output : AVAJ

Java Program : 

class string_rev
{
 static void reverse(String s)
 {
     String rev="";
     int i;
     for(i=s.length()-1;i>=0;i--)
     rev=rev+s.charAt(i);
     System.out.print("String input : "+s);
     System.out.print("\n\nReversed string : "+rev);
    }
}

4 comments:

  1. Say original s=hi my name
    And the output that we want=name my hi
    What to do in this
    case

    ReplyDelete
  2. What should I type in the parameters?

    ReplyDelete

ShareThis