Computer Applications

Write a program in Java to accept a String from the user. Pass the String to a method First(String str) which displays the first character of each word.
Sample Input : Understanding Computer Applications
Sample Output:
U
C
A

Java

User Defined Methods

25 Likes

Answer

import java.util.Scanner;

public class KboatFirstCharacter
{
    public void first(String str) {
        String t = " " + str;
        int len = t.length();
        
        for (int i = 0; i < len - 1; i++) {
            if (t.charAt(i) == ' ') {
                char ch = t.charAt(i + 1);
                System.out.println(ch);
            }
        }

    }
    
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = in.nextLine();
        
        KboatFirstCharacter obj = new KboatFirstCharacter();
        obj.first(s);
    }
}

Variable Description Table

Program Explanation

Output

Answered By

11 Likes


Related Questions