Computer Science, asked by Diya2003, 11 months ago

Write a program in Java to store 10 words in a single dimensional array. Display only those words which are palindrome .


Answer only if u are sure.

Answers

Answered by QGP
78
Here's the JAVA Code for the program. Description and explanations are mentioned as comments. Paste the code into an IDE, then the comments would be properly visible.


import java.util.Scanner;           //Importing Scanner
public class Brainly_Java           //Creating Class
{
    public static void main(String[] args)   //Creating main() function
    {
        Scanner sc = new Scanner(System.in);        //Creating Scanner object

        String arr[] = new String[10];              //Declaring a String Array of size 10

        for(int i=0;i<10;i++)               //Loop for taking User Input of 10 Words
        {
            System.out.print("Enter Word "+(i+1)+": ");     //Asking User to Input a Word
            arr[i] = sc.next();                             //Taking String Input
        }
        
        System.out.println("\nThe Palindrome Words are: \n");
        
        for(int i=0;i<10;i++)       //Loop for checking if a word is Palindrome, and printing if it is.
        {
            String str="";          //A new String variable. This will store the reverse of string in arr[i]
            
            for(int j=arr[i].length()-1;j>=0;j--)
            {
                str += arr[i].charAt(j);        //Storing each character of arr[i] in reverse
            }
                
            if(str.equalsIgnoreCase(arr[i]))        //If reverse string is equal to original string, then it is a Palindrome
            {
                System.out.println(arr[i]);         //Printing Palindrom Word
            }
        }

    }
    
}

Diya2003: Thx a lot.
QGP: You are welcome :)
Answered by Medhani272007
22
Hi There!!
Here's your answer!!!
Your answer is in the above picture...

By a Helper!!!
#BeBrainly

Attachments:
Similar questions