Write a program that reads a String and tests whether it begins with a vowel

Last update on May 28 2022 13:16:46 (UTC/GMT +8 hours)

Write a Python program to check whether an alphabet is a vowel or consonant.

Pictorial Presentation:

Write a program that reads a String and tests whether it begins with a vowel

Sample Solution:

Python Code:

l = input("Input a letter of the alphabet: ") if l in ('a', 'e', 'i', 'o', 'u'): print("%s is a vowel." % l) elif l == 'y': print("Sometimes letter y stand for vowel, sometimes stand for consonant.") else: print("%s is a consonant." % l)

Sample Output:

Input a letter of the alphabet: u u is a vowel.

Flowchart:

Write a program that reads a String and tests whether it begins with a vowel

Python Code Editor:

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to calculate a dog's age in dog's years.
Next: Write a Python program to convert month name to a number of days.

What is the difficulty level of this exercise?

Slice a Sequence:

>>> a = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20] >>> # Using a range, [start, end) >>> a[1:3] [2, 4] >>> # Using a range with a step >>> a[1:9:2] [2, 6, 10, 14] >>> # Leave out the start = an implicit start of 0 >>> a[:5] [0, 2, 4, 6, 8] >>> # Leave out the stop = an implicit end to the very last item >>> a[9:] [18, 20] >>> # Entire list >>> a[:] [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

So in event anyone ever comes across this and wants a easy compare method that can be used in many scenarios.

Doesn't matter if it is UPPERCASE or lowercase. A-Z and a-z.

bool vowel = ((1 << letter) & 2130466) != 0;

This is the easiest way I could think of. I tested this in C++ and on a 64bit PC so results may differ but basically there's only 32 bits available in a "32 bit integer" as such bit 64 and bit 32 get removed and you are left with a value from 1 - 26 when performing the "<< letter".

If you don't understand how bits work sorry i'm not going go super in depth but the technique of

1 << N is the same thing as 2^N power or creating a power of two.

So when we do 1 << N & X we checking if X contains the power of two that creates our vowel is located in this value 2130466. If the result doesn't equal 0 then it was successfully a vowel.

This situation can apply to anything you use bits for and even values larger then 32 for an index will work in this case so long as the range of values is 0 to 31. So like the letters as mentioned before might be 65-90 or 97-122 but since but we keep remove 32 until we are left with a remainder ranging from 1-26. The remainder isn't how it actually works, but it gives you an idea of the process.

Something to keep in mind if you have no guarantee on the incoming letters it to check if the letter is below 'A' or above 'u'. As the results will always be false anyways.

For example teh following will return a false vowel positive. "!" exclamation point is value 33 and it will provide the same bit value as 'A' or 'a' would.

Write a program that reads a String and tests whether it begins with a vowel
Harsh Jain

A vowel is a syllabic speech sound pronounced without any stricture in the vocal tract. The English alphabet has five vowels: A, E, I, O, U.

A String is an object in Java that represents a sequence of characters. The Java String class provides several methods to perform different operations.

To find the vowels in a given string, you need to compare every character in the given string with the vowel letters, which can be done through the charAt() and length() methods.

  • charAt(): The charAt() function in Java is used to read characters at a particular index number.
  • length(): The length() function in Java is used to find the length of a string, i.e., the number of characters present in the string.

Syntax

The code snippets below show the syntax of the length() and charAt functions, respectively.

public int length(); public char charAt(int index)

Code

The code below demonstrates how to find all the vowels in a string.

class Vowel { public static void main(String args[]) { String str = new String("Hi Welcome to my world!"); for(int i=0; i<str.length(); i++) { if(str.charAt(i) == 'a'|| str.charAt(i) == 'e'|| str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') System.out.println("Given string contains " + str.charAt(i)+" at the index " + i); } } }

Check all the vowels present in the string in Java

RELATED TAGS

java

string

communitycreator

Java program to identify whether the given character is a vowel or not. With the help of the below program, you will get to know how to write and print whether the given number is a vowel. We have written the program in three different ways, using if else statement, using switch case, user-defined method – learn more about switch case in Java with examples.

If you are good at programming even you can write the code in different formats. We are pretty sure that you can easily understand the following program. Just before that, we would like to share what are vowels and consonants in alphabets.

A: Def: A speech sound which is produced with the vibration of vocal chords without audible friction, which is being blocked by teeth, tongue or lips :P.

We know that there are Five vowels: a, e, i, o, u or A, I, E, O, U.

  • What are the consonants then?

Apart from A, E, I, O, U rest of the alphabets are consonants. That’s it.

Basically, we can write the code in two different ways, using if statement or if-else statement or Java switch case statement.

There you go:

Java Program To Print Whether The Given Alphabet is Vowel Or Consonant

1. Using Switch Case

Here we are using switch case, generally switch is used one out of multiple options, an if-else ladder can also be used to select one out of multiple options. In simple words, we can say the switch is a multi-branch statement.

Here the combination of both small letters and capital letters are represented as vowels. With the help of the case statement, the output will display on what the user is going to be entered. If it is out of 10 alphabets, it will display vowel or else consonant.

uisng switch case print vowel or consonant java

import java.util.Scanner; class Char { public static void main(String[ ] arg) { int i=0; Scanner sc=new Scanner(System.in); System.out.println("Enter a character : "); char ch=sc.next( ).charAt(0); //char ch=sc.nextChar(); switch(ch) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : case 'A' : case 'E' : case 'I' : case 'O' : case 'U' :i++; } if(i==1) System.out.println("Entered character "+ch+" is Vowel"); else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) System.out.println("Entered character "+ch+" is Consonent"); else System.out.println("Not an alphabet"); } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

import java.util.Scanner;

class Char

{

public static void main(String[ ] arg)

{

int i=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter a character : ");

char ch=sc.next( ).charAt(0);

//char ch=sc.nextChar();

switch(ch)

{

case  'a' :

case 'e'  :

case 'i'   :

case 'o'  :

case 'u'  :

case  'A' :

case 'E'  :

case 'I'   :

case 'O'  :

case 'U'  :i++;

}

if(i==1)

System.out.println("Entered character "+ch+" is  Vowel");

else

if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))

System.out.println("Entered character "+ch+" is Consonent");

else

System.out.println("Not an alphabet");

}

}

3 Sample Outputs:

output:

Enter a character : a Entered character a is Vowel

Enter a character :

a

Entered character a is  Vowel

output:

Enter a character : A Entered character A is Vowel

Enter a character :

A

Entered character A is  Vowel

output:

Enter a character : Z Entered character Z is Consonant

Enter a character :

Z

Entered character Z is Consonant

2.  Using If else Statement 

  • There you go another program using if else statement with sample outputs – learn more about if else statement here.

using if else statement

import java.util.Scanner; class Char { public static void main(String[ ] arg) { int i=0; Scanner sc=new Scanner(System.in); System.out.println("Enter a character : "); char ch=sc.next( ).charAt(0); if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') { System.out.println("Entered character "+ch+" is Vowel"); } else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) System.out.println("Entered character "+ch+" is Consonant"); else System.out.println("Not an alphabet"); } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

import java.util.Scanner;

class Char

{

public static void main(String[ ] arg)

{

int i=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter a character : ");

char ch=sc.next( ).charAt(0);

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')

{

System.out.println("Entered character "+ch+" is  Vowel");

}

else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))

System.out.println("Entered character "+ch+" is Consonant");

      else

System.out.println("Not an alphabet");

}

}

Output:

output:

Enter a character : G Entered character G is Consonant

Enter a character :

G

Entered character G is Consonant

Output:

Enter a character : 54 Not an alphabet

Enter a character :

54

Not an alphabet

 

3. Using Method

Java program to print whether the given character is vowel or consonant using Method or function with sample outputs.

Print vowel or consonant using method

import java.util.Scanner; class Char { void findVowelOrNot(char ch) { if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') { System.out.println("Entered character "+ch+" is Vowel"); } else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) System.out.println("Entered character "+ch+" is Consonent"); else System.out.println("Not an alphabet"); } public static void main(String[ ] arg) { Char c=new Char(); Scanner sc=new Scanner(System.in); System.out.println("Enter a character : "); char in=sc.next( ).charAt(0); c.findVowelOrNot(in); } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

import java.util.Scanner;

class Char

{

void findVowelOrNot(char ch)

{

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')

{

System.out.println("Entered character "+ch+" is  Vowel");

}

else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))

System.out.println("Entered character "+ch+" is Consonent");

      else

System.out.println("Not an alphabet");

}

public static void main(String[ ] arg)

{

Char c=new Char();

Scanner sc=new Scanner(System.in);

System.out.println("Enter a character : ");

char in=sc.next( ).charAt(0);

c.findVowelOrNot(in);

}

}

Output:

Enter a character : E Entered character E is Vowel

Enter a character :

E

Entered character E is  Vowel

Enter a character : 6 Not an alphabet

Enter a character :

6

Not an alphabet

That’s it. Here we go to the list of other programs: