KnowledgeBoat Logo

Computer Applications

Write a program in Java to accept a name(Containing three words) and Display only the initials (i.e., first letter of each word).
Sample Input: LAL KRISHNA ADVANI
Sample Output: L K A

Java

Java String Handling

179 Likes

Answer

import java.util.Scanner;

public class KboatNameInitials
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a name of 3 or more words:");
        String str = in.nextLine();
        int len = str.length();

        System.out.print(str.charAt(0) + " ");
        for (int i = 1; i < len; i++) {
            char ch = str.charAt(i);
            if (ch == ' ') {
                char ch2 = str.charAt(i + 1);
                System.out.print(ch2 + " ");
            }
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program in Java to accept a name(Containing three words) and Display only the initials (i.e., first letter of each word). Sample Input: LAL KRISHNA ADVANI Sample Output: L K A

Answered By

80 Likes


Related Questions