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…

ICSE Class 10 Computer Applications PYQ · 2022 Semester 2 · Section B

6Written · 2022 · Section B · 2022 Semester 2

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

Show SolutionHide Solution↓
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);
            }
        }
    }
}
BlueJ output of KboatWords.java
← Previous questionNext question →

Browse the free Computer Applications sample bank