Java Program to check if a vowel is present in the string?
brainlyprogrammer, copythat and moderators !!
Answers
Answer:
Check whether an alphabet is vowel or consonant using if..else statement
public class VowelConsonant {
public static void main(String[] args) {
char ch = 'i';
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )
System.out.println(ch + " is vowel");
else
System.out.println(ch + " is consonant");
}
}
Output
i is vowel
In the above program, 'i' is stored in a char variable ch. In Java, you use double quotes (" ") for strings and single quotes (' ') for characters.
Now, to check whether ch is vowel or not, we check if ch is any of: ('a', 'e', 'i', 'o', 'u'). This is done using a simple if..else statement.
We can also check for vowel or consonant using a switch statement in Java.
Answer:
import java.util.*;
class abc{
public static void main (String ar []){
Scanner sc= new Scanner (System.in);
System.out.println("Enter a string");
String s=sc.nextLine().toUpperCase();
int a=0;
for(int I=0;I<s.length();I++){
if((s.charAt(I)=='A')||(s.charAt(I)=='E')||(s.charAt(I)=='I')||(s.charAt(I)=='O')||(s.charAt(I)=='U')){
a++;
}
System.out.println((a>0)? "Vowel present":" Not present ") ;
}
}
}
Logic:-
- Accept a string
- Check if any character is a vowel or not using charAt()
- if yes, a++
- when loop ends check if a>0
- yes? print "yes, present" otherwise print "not found."
- Wohoo! You made it!