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 Function Overload 2016 Q6 Solved

Tuesday, September 19, 2023

ICSE Java Programming Function Overload 2016 Q6 Solved

 Special words are those words which starts and ends with the same letter. 

Examples:
EXISTENCE
COMIC
WINDOW
Palindrome words are those words which read the same from left to right and vice versa
Examples:
MALAYALAM
MADAM
LEVEL
ROTATOR
CIVIC
All palindromes are special words, but all special words are not palindromes. Write a program to accept a word check and print Whether the word is a palindrome or only special word.

import java.util.Scanner;

public class PalindromeOrSpecialWord {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a word: ");
        String word = scanner.nextLine();
        scanner.close();

        if (isPalindrome(word) && isSpecial(word)) {
            System.out.println("The word is both a palindrome and a special word.");
        } else if (isPalindrome(word)) {
            System.out.println("The word is a palindrome.");
        } else if (isSpecial(word)) {
            System.out.println("The word is a special word.");
        } else {
            System.out.println("The word is neither a palindrome nor a special word.");
        }
    }

    // Method to check if a word is a palindrome
    public static boolean isPalindrome(String word) {
        String reversed = "";
        for (int i = word.length() - 1; i >= 0; i--) {
            reversed += word.charAt(i);
        }
        return word.equalsIgnoreCase(reversed);
    }

    // Method to check if a word is a special word
    public static boolean isSpecial(String word) {
        return word.length() >= 2 && word.charAt(0) == word.charAt(word.length() - 1);
    }
}

No comments:

Post a Comment