Showing posts with label reverse string. Show all posts
Showing posts with label reverse string. Show all posts

Tuesday, April 16, 2013

Reverse String : Java OOP

Write Java Source code: Reverse String in Java Programming

Sample Output:
Enter a word: arif
The reverse word is: fira


Source Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Java source code on how to print a string backward using recursion
//java class
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();
    }
}