KnowledgeBoat Logo

Computer Applications

Write a program in Java to store 10 words in a Single Dimensional Array. Display only those words which are Palindrome.
Sample Input: MADAM, TEACHER, SCHOOL, ABBA, ………
Sample Output: MADAM
ABBA
……….
……….

Java

Java String Handling

53 Likes

Answer

import java.util.Scanner;

public class KboatSDAPalindrome
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        String words[] = new String[10];
        System.out.println("Enter 10 words:");

        for (int i = 0;  i < words.length; i++) {
            words[i] = in.nextLine();
        }
        
        System.out.println("\nPalindrome Words:");

        for (int i = 0;  i < words.length; i++) {
            String str = words[i].toUpperCase();
            int strLen = str.length();
            boolean isPalin = true;

            for (int j = 0; j <  strLen / 2; j++) {
                if (str.charAt(j) != str.charAt(strLen - 1 - j)) {
                    isPalin = false;
                    break;
                }
            }

            if (isPalin)
                System.out.println(words[i]);
            
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program in Java to store 10 words in a Single Dimensional Array. Display only those words which are Palindrome. Sample Input: MADAM, TEACHER, SCHOOL, ABBA, ……… Sample Output: MADAM ABBA ………. ……….

Answered By

14 Likes


Related Questions