Reverse string Or Print String Backward using Recursion


public class StringBackward
{
    public static void reverseString(String word, int size)
    {
       if(size==0)
       {
           return;
       }
       else
       {
          System.out.print(word.charAt(size-1));
          reverseString(word, size-1);
       }
    }

}

//main class

import java.util.Scanner;

public class Main {

    public static void main(String[] args)
    {
      Scanner input = new Scanner(System.in);

       String word;
       System.out.print("Enter a word: ");
       word = input.next();

        StringBackward access = new  StringBackward();
        System.out.print("The reverse word is: ");
        access.reverseString(word, word.length());
        System.out.println();

    }

}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|