Palindrome Program in java

Question:

Astrologist believes that having a palindromic name is very auspicious. As we all know, a palindrome is a word that can be read the same way in either direction. There should not be a space or any special character in the word entered. If yes, display “Invalid Input”. Write a Java program to determine whether a given word is a palindrome or not.

Sample Input 1:

Enter the word : 

Malayalam

Sample Output 1: 

Malayalam is a Palindrome

Sample Input 2:

Enter the word : 

Apple

Sample Output 2: 

Apple is not a Palindrome

Sample Input 3:

Enter the word :

no on

Sample Output 3: 

Invalid Input

Sample Input 4:

Enter the word :

@nnn

Sample Output 4: 

Invalid Input

CODE:

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Palindrome 
{
    public static void main (String[] args) {
        Scanner sc= new Scanner(System.in);
        
        System.out.println("Enter the word :");
        String str=sc.nextLine();
        int len=str.length();
        
        Pattern p=Pattern.compile("[^a-z0-9]", Pattern.CASE_INSENSITIVE);
        Matcher m=p.matcher(str);
        boolean b=m.find();
       
        if(b)
        {
            System.out.println("Invalid Input");
        }
        else
        {
            String rvs=new String();
            
            for(int i=(len-1); i>=0; i--)
            {
                rvs=rvs+str.charAt(i);
            }
            
            if(str.equalsIgnoreCase(rvs))
            {
                System.out.println(str+" is a Palindrome");
            }
            else
            {
                System.out.println(str+" is not a Palindrome");
            }
        }
    }
}
public class Student 
{
    private int id;
    private String name;
    private int[] marks;
    private float average;
    private char grade;
    
    public void setId(int id)
    {
        this.id=id;
    }
    
    public int getId()
    {
        return this.id;
    }
    
    public void setName(String name)
    {
        this.name=name;
    }
    
    public String getName()
    {
        return this.name;
    }
    
    public void setMarks(int[] marks)
    {
        this.marks=marks;
    }
    
    public int[] getMarks()
    {
        return this.marks;
    }
    
    public void calculateAvg()
    {   float sum=0;
        
        for(int i=0;i<this.marks.length;i++)
        {
            sum+=this.marks[i];
        }
        
        average=(float)(sum/(this.marks.length));
        setAverage(average);
    }
    
    public void findGrade()
    {int flag=0;
        for(int i=0;i<this.marks.length;i++)
        {
            if(this.marks[i]<50)
            {
                flag++;
            }
        }
        if(flag>0)
        {
            grade='F';
        }
        else
        {
            if(this.average>=80 && this.average<=100)
            {
                grade='O';
            }
            else if(this.average>=50 && this.average<=79)
            {
                grade='A';
            }
            else
            {
                grade='F';
            }
        }
        setGrade(grade);
        
    }
    
    public void setAverage(float average)
    {
        this.average=average;
    }
    
    public void setGrade(char grade)
    {
        this.grade=grade;
    }
    
    public float getAverage()
    {
        return this.average;
    }
    
    public char getGrade()
    {
        return this.grade;
    }
}
Next:
  1. Numerology
  2. InitCap
  3. Array Compatiblilty
  4. Sum of the maximum and the minimum element

Leave a Comment

Scroll to Top