Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ICSE Java Programming palindrome 2022 Q6 SPECIMEN PAPER Solved

Sunday, January 8, 2023

ICSE Java Programming palindrome 2022 Q6 SPECIMEN PAPER Solved

ICSE Java Programming Palindrome strings  2022 Q6 SPECIMEN PAPER Solved

Define a class to accept a string, convert it into lowercase and check whether the string is a palindrome or not. A palindrome is a word which reads the same backward as forward.

Example: madam, racecar


import java.util.Scanner;

public class ICSEJava {
    public static void main(String[] args) {
        Palindrome obj = new Palindrome();
        obj.inputString();
        obj.checkPalindrome();
    }
}

class Palindrome {
    public void checkPalindrome() {
        for (int i = 0; i < this.message.length(); i++) {
            if (this.message.charAt(i) != this.message.charAt(this.message.length() - 1 - i)) {
                flag = false;
                break;
            } else
                continue;
        }
        if (flag)
            System.out.println("Entered word is a palindrome:" + this.message);
        else
            System.out.println("Entered word is not a palindrome.");
    }

    public void inputString() {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the word:");
        this.message = in.nextLine();
        this.message = this.toLowerCase(message);
        in.close();
    }

    private String toLowerCase(String msg) {
        String temp = "";
        for (int i = 0; i < msg.length(); i++) {
            if (msg.charAt(i) >= 'A' && msg.charAt(i) <= 'Z') {
                temp += (char) (msg.charAt(i) + 32);
            } else
                temp += (msg.charAt(i));
        }
        return temp;
    }

    Palindrome() {
        this.message = "";
        this.flag = true;
    }

    private boolean flag;
    private String message;
}

No comments:

Post a Comment