-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRevStrUStk.java
More file actions
60 lines (39 loc) · 780 Bytes
/
RevStrUStk.java
File metadata and controls
60 lines (39 loc) · 780 Bytes
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//Reverse String Using Statck
/// WAP to reverse a string or Array using Stack
import java.util.*;
public class RevStrUstk
{
public static void main(String[] args)
{
int top=0,n,i;
Scanner s=new Scanner(System.in);
String ss;
System.out.println("Enter the String! ");
ss=s.nextLine();
char a[]=ss.toCharArray();
n=a.length;
char stack[]=new char[n];
for(i=0;i<n;i++)
{
stack[top]=a[i];
if(top==(n-1))
{
break;
}
top++;
}
// create a loop for Pop fuction
for(i=0;i<n;i++)
{
a[i]=stack[top];
if(top<0)
{
break;
}
top--;
}
// now print array and get reverse order
System.out.print("Reverse String :! \t");
System.out.print(String.valueOf(a));
}
}