Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Sunday 21 January 2018

java program to check Palindrome

In This program I use stack to store string

by using stack API

import java.util.Stack; import java.util.Scanner; class PalindromeTest { public static void main(String[] args) { System.out.print("Enter any string:"); Scanner in=new Scanner(System.in); String inputString = in.nextLine(); Stack stack = new Stack(); for (int i = 0; i < inputString.length(); i++) { stack.push(inputString.charAt(i)); } String reverseString = ""; while (!stack.isEmpty()) { reverseString = reverseString+stack.pop(); } if (inputString.equals(reverseString)) System.out.println("The input String is a palindrome."); else System.out.println("The input String is not a palindrome."); } }

No comments:

Post a Comment