» » Check whether string is palindrome or not


1.Palindrome program in java using for loop

 

  1. package com.instaceofjava;
  2.  
  3. public class PalindromeDemo{  
  4.  
  5. public static void main(String[] args) {
  6.  
  7. String str="MADAM";
  8. String revstring="";
  9.  
  10. for(int i=str.length()-1;i>=0;--i){
  11. revstring +=str.charAt(i);
  12. }
  13.  
  14. System.out.println(revstring);
  15.  
  16. if(revstring.equalsIgnoreCase(str)){
  17. System.out.println("The string is Palindrome");
  18. }
  19. else{
  20. System.out.println("Not Palindrome");
  21. }
  22.  
  23. }
  24. }


  Output:

  1. The string is Palindrome

 

2.palindrome program in java using stringbuffer

 

  1. package com.instaceofjava;
  2. import java.util.Scanner;
  3.  
  4. public class Palindrome {
  5.  
  6. public static void main(String[] args)
  7. {
  8.  
  9.  Scanner in = new Scanner(System.in);
  10.  System.out.println("Enter a string");
  11.  String str=in.nextLine();
  12.  
  13.  StringBuffer strone=new StringBuffer(str);
  14.  StringBuffer strtwo=new StringBuffer(strone);
  15.  
  16.   strone.reverse();
  17.  
  18.   System.out.println("Orginal String ="+strtwo);
  19.   System.out.println("After Reverse ="+strone);
  20.  
  21.  if(String.valueOf(strone).compareTo(String.valueOf(strtwo))==0)
  22.    System.out.println("Result:Palindrome");
  23.     else
  24.     System.out.println("Result:Not Palindrome");
  25.  
  26.     }
  27. }

 Output:

  1. Enter a string
  2. MOOM
  3. Orginal String =MOOM
  4. After Reverse =MOOM
  5. Result:Palindrome

Simple palindrome program in java:

3.Program to Check given number is palindrome or not

  1. package com.instaceofjava;
  2. import java.util.Scanner;
  3.  
  4. public class Palindrome {
  5.  
  6. public static void main(String[] args)
  7. {
  8.  
  9.  System.out.println("Please Enter a number : ");
  10.  
  11.         int givennumber = new Scanner(System.in).nextInt();
  12.  
  13.         int number=givennumber;
  14.         int reverse=0;
  15.  
  16.         while (number != 0) {
  17.  
  18.             int remainder = number % 10;
  19.             reverse = reverse * 10 + remainder;
  20.             number = number / 10;
  21.         }            
  22.  
  23.  if(givennumber == reverse)
  24.    System.out.println("Result:Palindrome");
  25.     else
  26.     System.out.println("Result:Not Palindrome");
  27.  
  28.     }
  29. }

 Output:

  1. Please Enter a number : 
  2. 535
  3. Result:Palindrome


Java programming interview questions
  1. Print prime numbers? 
  2. Dynamic polymorphism in Java 
  3. Find missing numbers in an array
  4. Collection vs Collections
  5. Custom iterator in java 
  6. Unreachable Blocks in java
  7. Enum in java  
  8. toString() method in java with example program
  9. Explain Hibernate Configuration file
  10. What happens if we place return statement in try catch blocks 
  11. Write a java program to convert binary to decimal 
  12. Java interview questions on exception handling 
  13. Java Program to convert Decimal to Binary
  14. Java program to restrict a class from creating not more than three objects
  15. Java basic interview programs on this keyword 
  16. Interfaces allows constructors? 
  17. Can we create static constructor in java 
  18. Super keyword interview questions java 
  19. Java interview questions on final keyword
  20. Can we create private constructor in java
  21. Java Program Find Second highest number in an integer array 
  22. Java interview programming questions on interfaces 
  23. Top 15 abstract class interview questions  
  24. Java interview Questions on main() method  
  25. Top 20 collection framework interview Questions
  26. Java Interview Program to find smallest and second smallest number in an array 
  27. Java Coding Interview programming Questions : Java Test on HashMap  
  28. Explain java data types with example programs 
  29. Constructor chaining in java with example programs 
  30. Swap two numbers without using third variable in java 
  31. Find sum of digits in java 
  32. How to create immutable class in java 
  33. AtomicInteger in java 
  34. Check Even or Odd without using modulus and division  
  35. String Reverse Without using String API 
  36. Find Biggest substring in between specified character
  37. Check string is palindrome or not?
  38. Reverse a number in java?
  39. Fibonacci series with Recursive?
  40. Fibonacci series without using Recursive?
  41. Sort the String using string API?
  42. Sort the String without using String API?
  43. what is the difference between method overloading and method overriding?
  44. How to find largest element in an array with index and value ?
  45. Sort integer array using bubble sort in java?
  46. Object Cloning in java example?
  47. Method Overriding in java?
  48. Program for create Singleton class?
  49. Print numbers in pyramid shape?
  50. Check armstrong number or not?
  51. Producer Consumer Problem?
  52. Remove duplicate elements from an array
  53. Convert Byte Array to String
  54. Print 1 to 10 without using loops
  55. Add 2 Matrices
  56. Multiply 2 Matrices
  57. How to Add elements to hash map and Display
  58. Sort ArrayList in descending order
  59. Sort Object Using Comparator
  60. Count Number of Occurrences of character in a String
  61. Can we Overload static methods in java
  62. Can we Override static methods in java 
  63. Can we call super class static methods from sub class 
  64. Explain return type in java 
  65. Can we call Sub class methods using super class object? 
  66. Can we Override private methods ? 
  67. Basic Programming Questions to Practice : Test your Skill
  68. Java programming interview questions on collections

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

3 comments for Check whether string is palindrome or not

  1. public static void isPalindrome(String test) {
    for(int i = 0; i < test.length()/2; i++) {
    if(test.charAt(i) != test.charAt(test.length() - (i + 1))) {
    System.out.println("false");
    return;
    }
    }
    System.out.println("true");
    }

    ReplyDelete
  2. how to remove duplicate character from String
    String s="aabbccdd";
    output sholud be=abcd

    ReplyDelete
  3. To consider the spaces in between.i.e. "Put it on" string is also a palindrome, little enhancement to the above code. just replace line no 12
    "String str=in.nextLine();" with
    String str=in.nextLine().replace(" ", "");

    ReplyDelete

Select Menu