reverse a string using stacks
class reverse_a_string_using_stack { public static void main(String[] args) { String input="vinayak"; reverse(input); } static String input="vinayak"; static int l=input.length(); static char array[]=new char[l]; static int top=-1; static char stackarray[]=new char[l]; static void reverse(String input) { for (int j = 0; j < l; j++) { char ch = input.charAt(j); push(ch); } if(top>=0) { System.out.println("Elements in the Stack "); for(int i=0;i<l;i++) { System.out.println(" --->>>"+stackarray[i]); } } System.out.println(" Reversed String is = ."); for (int i = 0; i <l; i++) { pop(); } } public static void push(char value) { top++; stackarray[top]=value; } public static void pop() { if(top>=0) { System.out.print(stackarray[top]); top--; } } }
reverse a string using stacks
Reviewed by Unknown
on
03:39
Rating:
No comments: