java programs

Java program to check whether string is palindrome or not

In earlier post we have discussed how to check whether a number is palindrome or not.In today’s post we shall check whether a string is palindrome or not.For this we shall use built in functions to reverse a string and then compare the reversal of string to given string.
Procedure:1. Take input from the user and store it in a variable.
2.Here we are going to call a function “reveresing” to find the reverse of the string.
3.At last we shall call the display function to show whether it is a palindrome or not.

Now we shall write a program and get a detailed explanation after this.
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
import java.util.*;
class finding
{
   String str,reverse_str="";
   int i;
  finding()
   {
     System.out.print("Enter any string:");
     Scanner in=new Scanner(System.in);
     str = in.nextLine();
    }
   void reversing()
    {
      int len_str = str.length();
      for(i=len_str-1;i>=0;i--)
          reverse_str = reverse_str+str.charAt(i);
      System.out.println("Hence reverse of the string is:"+reverse_str);
     }
    void display()
      {
         if(str.equals(reverse_str))
           System.out.println("Given string " + str + "  is palindrome");
         else
           System.out.println("Given string "+ str +" is not palindrome");
       }
}
         
class strpalin
{
     public static void main(String a[])
    {
       finding ob=new finding();
       ob.reversing();
       ob.display();
    }
}
Explanation:1. In the “reversing method” we are calculating the length of the given string.Then we are adding each letter of the given string to “reverse_str” variable.Each letter of the given string is obtained using “variable.charAt()” method.
For Ex: If the given string is “hello” then by using str.charAt(2) we get l as the output.Because of indexing of the string begins from 0.
3.Therefore a reverse of the given string is obtained.
4. Now we shall compare this reverse string with given string and display accurate message.

Java program to check palindrome


Java palindrome program: Java program to check if a string is a palindrome or not. Remember a string is a palindrome if it remains unchanged when reversed, for example "dad" is a palindrome as reverse of "dad" is "dad" whereas "program" is not a palindrome. Some other palindrome strings are "mom", "madam", "abcba".

Java programming source code

import java.util.*;
 
class Palindrome
{
   public static void main(String args[])
   {
      String original, reverse = "";
      Scanner in = new Scanner(System.in);
 
      System.out.println("Enter a string to check if it is a palindrome");
      original = in.nextLine();
 
      int length = original.length();
 
      for ( int i = length - 1; i >= 0; i-- )
         reverse = reverse + original.charAt(i);
 
      if (original.equals(reverse))
         System.out.println("Entered string is a palindrome.");
      else
         System.out.println("Entered string is not a palindrome.");
 
   }
} 

Another method to check palindrome:
import java.util.*;
 
class Palindrome
{
  public static void main(String args[])
  {
    String inputString;
    Scanner in = new Scanner(System.in);
 
    System.out.println("Input a string");
    inputString = in.nextLine();
 
    int length  = inputString.length();
    int i, begin, end, middle;
 
    begin  = 0;
    end    = length - 1;
    middle = (begin + end)/2;
 
    for (i = begin; i <= middle; i++) {
      if (inputString.charAt(begin) == inputString.charAt(end)) {
        begin++;
        end--;
      }
      else {
        break;
      }
    }
    if (i == middle + 1) {
      System.out.println("Palindrome");
    }
    else {
      System.out.println("Not a palindrome");
    }  
  }
}
Both the above codes consider string as case sensitive, you can modify them so that they ignore the case of string. You can either convert both strings to lower or upper case for this. But do not modify original strings as they may be further required in program.

Comments

Popular posts from this blog

cpanel exam CPSP Answers

How to install zimbra collaboration suite 8.8.11 on CentOS 7

awstats installation