KnowledgeBoat Logo

Computer Applications

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, John, Farha, AMITHA
Output: Anita
Amrita
Alina
AMITHA

Java

Java Arrays

ICSE 2022

23 Likes

Answer

import java.util.Scanner;

public class KboatWords
{
    public static void main(String args[]) 
    {
        Scanner in = new Scanner(System.in);
        String names[] = new String[10];
        int l = names.length;
        System.out.println("Enter 10 names : ");
        
        for (int i = 0; i < l; i++) 
        {
            names[i] = in.nextLine();
        }
        
        System.out.println("Names that begin and end with letter A are:");

        for(int i = 0; i < l; i++)
        {
            String str = names[i];
            int len = str.length();
            char begin = Character.toUpperCase(str.charAt(0));
            char end = Character.toUpperCase(str.charAt(len - 1));
            if (begin == 'A' && end == 'A') {
                System.out.println(str);
            }
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of 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, John, Farha, AMITHA Output: Anita Amrita Alina AMITHA

Answered By

9 Likes


Related Questions