How to reverse the string in JAVA?

0 votes
57 views
asked Jun 18, 2014 in JAVA by anonymous
Hi all

  PLease provide a small programme for reverse the string?

1 Answer

0 votes
answered Jun 18, 2014 by ram

Hi 

 

  

 

import java.util.*;

import java.io.*;

 

public class Function {

    String FirstReverse(String str) {

        

        // code goes here

        /* Note: In Java the return type of a function and the

         parameter types being passed are defined, so this return

         call must match the return type of the function.

         You are free to modify the return type. */

        int length = str.length();

        

        char[] myNameChars = str.toCharArray();

        

        

        

        for(int i = length-1; i >= length/2;i--){

            char last = str.charAt(i);

            char first = str.charAt(length - 1 - i);

            

            myNameChars[length - 1 -i] = last;

            myNameChars[i] = first;

            

        }

        

        str = String.valueOf(myNameChars);

        return str;

        

    }

    

    public static void main (String[] args) {

        // keep this function call here

        Scanner  s = new Scanner(System.in);

        Function c = new Function();

        System.out.print(c.FirstReverse(s.nextLine())); 

    }   

    

}

...