CJ_Slip_2A

Slip No.2

A) Write a java program to display all the vowels from a given string.

1. Open Notepad.

2. Type the following code:

    import java.util.Scanner;

    public class DisplayVowels {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter a string:");
    String input = scanner.nextLine();

    System.out.println("Vowels in the given string:");
    for (int i = 0; i < input.length(); i++) {
    char c = input.charAt(i);
    if (isVowel(c)) {
    System.out.print(c + " ");
    }
    }
    }

    public static boolean isVowel(char c) {
    c = Character.toLowerCase(c);
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
    }
    }


3. Save the file with the name DisplayVowels.java. Make sure to select "All Files" in the "Save as type" dropdown and add the .java extension manually.

4. Open the Command Prompt.

5. Compile the Java program by typing:
javac DisplayVowels.java

6. Run the compiled Java program by typing:
java DisplayVowels

7. output:
Enter a string:
Hello World
Vowels in the given string:
e o o

No comments:

Post a Comment