Reversing a string using stack in java....
here is the code..
import java.util.*;
import java.io.*;
public class Reverse {
public static void main(String args[]) throws Exception {
String name = "gaurav";
int k = name.length();
Stack ab = new Stack();
char[] name1 = new char[k];
for (int i = 0; i < k; i++)
ab.push(new Character(name.charAt(i)));
for (int j = 0; j < k; j++)
name1[j] = ((Character) ab.pop()).charValue();
for (char s: name1)
System.out.print(s);
}
}
No comments:
Post a Comment