Write a java program to display all the vowels from a given string | EasyCoding45

Write a Java program to display all the vowels from a given string


Here's a simple Java program that takes a string as input and displays all the vowels present in that string:


import java.util.Scanner;

public class DisplayVowels {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Input string from the user
        System.out.print("Enter a string: ");
        String inputString = scanner.nextLine();

        // Displaying vowels in the input string
        System.out.print("Vowels in the given string: ");
        displayVowels(inputString);

        scanner.close();
    }

    private static void displayVowels(String input) {
        // Convert the input string to lowercase to simplify vowel checking
        input = input.toLowerCase();

        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);

            // Check if the character is a vowel
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                System.out.print(ch + " ");
            }
        }
    }
}

Note : This program prompts the user to enter a string, converts it to lowercase to simplify vowel checking, and then iterates through each character to display the vowels. You can copy and paste this code into a Java file (e.g., DisplayVowels.java) and then compile and run it using a Java compiler.

Output :
https://easycoding45.blogspot.com/2023/12/write-java-program-to-display-all.html


Comments