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();
    }
}

9 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Mohammad Ashraful Hasan Sobuj
    My Batch- 46th
    My Department :CSE
    My ID 201420656


    public class StringSetward
    {
    public static void unsetString(String word, int size)
    {
    if(size==0)
    {
    return;
    }
    else
    {
    System.out.print(word.charAt(size-1));
    unsetString(word, size-1);
    }
    }
    }
    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();
    StringSetward access = new StringSetward();
    System.out.print("The unset word is: ");
    access.unsetString(word, word.length());
    System.out.println();
    }
    }

    ReplyDelete
  3. sultana razia khan
    My Batch- 46th
    My Department :CSE
    My ID 201420715



    public class Stringbackward
    {
    public static void unreseString(String word, int size)
    {
    if(size==0)
    {
    return;
    }
    else
    {
    System.out.print(word.charAt(size-1));
    unreseString(word, size-1);
    }
    }
    }
    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 unrese word is: ");
    access.unreseString(word, word.length());
    System.out.println();
    }
    }

    ReplyDelete
  4. Sharmin Akter
    My Batch- 46th
    My Department :CSE
    My ID 201420769


    public class StringSetward
    {
    public static void unsetString(String word, int size)
    {
    if(size==0)
    {
    return;
    }
    else
    {
    System.out.print(word.charAt(size-1));
    unsetString(word, size-1);
    }
    }
    }
    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();
    StringSetward access = new StringSetward();
    System.out.print("The unset word is: ");
    access.unsetString(word, word.length());
    System.out.println();
    }
    }

    ReplyDelete
  5. MD: Rashed Prodhania (Jewel)
    ID: 201420763
    Batch : 46th

    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);
    }
    }

    }




    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();

    }

    }

    ReplyDelete
  6. StringBackward access = new StringBackward();

    Can anyone explain the work of access following this line plz?

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. ID-201420406
    Batch-46th

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

    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 unrese word is: ");
    access.unreseString(word, word.length());
    System.out.println();
    }
    }

    ReplyDelete
  9. Name : Jalis Mahmud
    ID :201420762
    Batch : 46th

    public class stringsetward
    {
    public static void unreseString(String word, int size)
    {
    if(size==0)
    {
    return;
    }
    else
    {
    System.out.print(word.charAt(size-2));
    unreseString(word, size-2);
    }
    }
    }
    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 unrese word is: ");
    access.unreseString(word, word.length());
    System.out.println();
    }
    }

    ReplyDelete