Choose Your Language

Thursday, 4 April 2013

program to reverse a string

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class StringReverse {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
         String original, reverse = "";
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
          try {
              System.out.println("Enter a string to reverse");
            original = br.readLine();           
            int length=original.length();
               for ( int i = length - 1 ; i >= 0 ; i-- )
                 reverse = reverse + original.charAt(i);
       
                   System.out.println("Reverse of entered string is: "+reverse);
              
           
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
         
       }   

}

output:
Enter a string to reverse
aravind sankaran
Reverse of entered string is: naraknas dnivara

No comments:

Post a Comment