Sunday, July 9, 2023

Define a class to declare an array to accept and store ten words. Display only those words which begin with the letter 'A' or 'a' and also end with the letter 'A' or 'a'.

Define a class to declare an array to accept and store ten words. Display only those words which begin with the letter 'A' or 'a' and also end with the letter 'A' or 'a'.

EXAMPLE:
Input: Hari, Anita, Akash, Amrita, Alina, Devi, Rishab, Jolin, Farha, AMITHA
Output:
Anita
Amrita
Alina
AMITHA

Solution:
import java.util.Scanner;

public class Year2022Q2
{
    public static void main(String[] args)
    {
        String names[] = new String[10];
        Scanner sc = new Scanner(System.in);
        int len = names.length;
        System.out.println("Enter 10 name:");
        for (int i = 0; i < len; i++)
        {
             names[i]=sc.nextLine();
        }
        System.out.println("Names are:");
        for (int i = 0; i < len; i++)
        {
            int lastIndex = names[i].length() - 1;
            if((names[i].charAt(0)=='a'
                ||names[i].charAt(0)=='A')
                &&(names[i].charAt(lastIndex)=='a'
                ||names[i].charAt(lastIndex)=='A'))
            {
                System.out.println(names[i]);
            }
        }
    }
}

Output:



No comments:

Post a Comment

Check whether the word is Special word or Palindrome word.

Special words are those words which starts and ends with the same letter.  Examples: EXISTENCE COMIC WINDOW Palindrome words are those words...