Computer Applications

Write a program in Java to accept a String from the user. Pass the String to a method Display(String str) which displays the consonants present in the String.

Sample Input: computer
Sample Output:
c
m
p
t
r

Java

User Defined Methods

18 Likes

Answer

import java.util.Scanner;

public class KboatConsonants
{
    public void display(String str) {
        
        String t = str.toUpperCase();
        int len = t.length();
        
        for (int i = 0; i < len; i++) {
            char ch = t.charAt(i);
            if (ch != 'A' &&
                ch != 'E' &&
                ch != 'I' &&
                ch != 'O' &&
                ch != 'U') {
                System.out.println(str.charAt(i));
            }
        }
    }
    
     public static void main(String args[]) { 
        Scanner in = new Scanner(System.in);
        System.out.print("Enter string: ");
        String s = in.nextLine();
        
        KboatConsonants obj = new KboatConsonants();
        obj.display(s);
    }
}

Variable Description Table

Program Explanation

Output

Answered By

9 Likes


Related Questions