generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome.java
41 lines (40 loc) · 983 Bytes
/
palindrome.java
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
import java.util.*;
class stack{
char arr[];
int top;
public stack(int n){
arr = new char[n];
top = -1;
}
public void push(char a){
arr[++top] = a;
}
public char pop(){
return(arr[top--]);
}
public char peek(){
return(arr[top]);
}
}
public class palindrome{
public static void main(String args[]){
String word;
String temp = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter the String:");
word = sc.nextLine();
stack s = new stack(word.length());
for(int i=0;i<word.length();i++){
s.push(word.charAt(i));
}
for(int i=0;i<word.length();i++){
temp = temp + s.pop();
}
if(word.compareTo(temp) == 0){
System.out.println("the string is a palindrome");
}
else{
System.out.println("the string is not a palindrome");
}
}
}