Computer Applications

Write a program to accept 10 names in a Single Dimensional Array (SDA). Display the names whose first letter matches with the letter entered by the user.

Sample Input:
Aman Shahi
Akash Gupta
Suman Mishra
and so on ……….

Sample Output:
Enter the alphabet: A
Aman Shahi
Akash Gupta
…..
…..

Java

Java String Handling

48 Likes

Answer

import java.util.Scanner;

public class KboatSDANames
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        String names[] = new String[10];
        System.out.println("Enter 10 names");
        
        for (int i = 0; i < names.length; i++) {
            names[i] = in.nextLine();
        }
        
        System.out.print("Enter a letter: ");
        char ch = in.next().charAt(0);
        ch = Character.toUpperCase(ch);
        
        for (int i = 0; i < names.length; i++) {
            if (Character.toUpperCase(names[i].charAt(0)) == ch) {
                System.out.println(names[i]);
            }
        }
    }
}

Variable Description Table

Program Explanation

Output

Answered By

16 Likes


Related Questions