KnowledgeBoat Logo

Computer Applications

Write a program using a method Palin( ), to check whether a string is a Palindrome or not. A Palindrome is a string that reads the same from the left to right and vice versa.
Sample Input: MADAM, ARORA, ABBA, etc.

Java

User Defined Methods

ICSE 2007

88 Likes

Answer

import java.util.Scanner;

public class KboatStringPalindrome
{
    public void palin() {
        
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the string: ");
        String s = in.nextLine();
        
        String str = s.toUpperCase();
        int strLen = str.length();
        boolean isPalin = true;
        
        for (int i = 0; i <  strLen / 2; i++) {
            if (str.charAt(i) != str.charAt(strLen - 1 - i)) {
                isPalin = false;
                break;
            }
        }
        
        if (isPalin)
            System.out.println("It is a palindrome string.");
        else
            System.out.println("It is not a palindrome string.");
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program using a method Palin( ), to check whether a string is a Palindrome or not. A Palindrome is a string that reads the same from the left to right and vice versa. Sample Input: MADAM, ARORA, ABBA, etc.

Answered By

31 Likes


Related Questions