String Reverse – Recursion Program in java


 

Question:

Ram participates in an online gaming contest, where he is asked to code logic in Java to reverse the given word using recursion. Help Ram to perform the same. Check the sample input and output statements for more clarifications.

Function signature should be: public static String reverseString(String str)

Implement the main() inside the class ‘ReverseDriver’

Sample Input/Output 1:

Enter the string: teknoturf

Reversed string is: frutonket

Sample Input/Output 2:

Enter the string: krishna

Reversed string is: anhsirK

Sample Input/Output3:

Enter the string: Hello World

Reversed string is: dlroW olleH

CODE:

import java.util.Stack;
import java.util.Scanner;
class StackDriver
{ static Stack<Integer>st=new Stack<>();
  static void insert_at_bottom(int x)
  { if(st.isEmpty())
       st.push(x);
    else
    {  int a= st.peek();
       st.pop();
       insert_at_bottom(x);
       st.push(a);
    }
}
static void reverse()
{ if(st.size()>0)
  { int x= st.peek();
    st.pop();
    reverse();
    insert_at_bottom(x);
  }
}
public static void main(String[] args)
{ Scanner s=new Scanner(System.in);
  System.out.println("Enter length of List:");
  int n=s.nextInt();
  if(n==0||n<0)
  { System.out.println("Invalid Length");}
  else
    {for(int i=1;i<=n;i++)
        { st.push(i);}
          System.out.println("Elements in Stack ");
          System.out.println(st);
          reverse();
          System.out.println("Elements in the stack after reversal");
          System.out.println(st);}
}
}


data structures programs in java pdf | data structures programs in javatpoint | data structures course in java | data structures and algorithms programs in java |best data structures course in java | data structures programs using java | advanced data structures lab programs in java | data structures and algorithms course in java | basic data structure programs in java | c data structures programs | data structures example programs in java | data structures in java free course |graph data structure program in java | data structures using java | interview programs on data structures in java | linked list data structure program in java | data structure programs on java |

data structures in java online course | queue data structure program in java | data structure programming questions in java | data structures in java coding questions |
data structure coding interview questions java | stack data structure program in java | tree data structure programs in java | tree data structure code in java |
data structures and java | java with data structures programs | data structures programs pdf | amazon | microsoft | google | 2021
Previous
Next Post »