Count total vowels in string in java.
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter string=");
String str=sc.nextLine();
int n=str.length();
int count=0;
for(int i=0; i<n; i++){
char ch=str.charAt(i);
if(isvowel(ch)==true)
count++;
}
System.out.println("total vowels are="+count);
}
public static boolean isvowel(char ch){
if(ch=='a' || ch=='A') return true;
if(ch=='e' || ch=='E') return true;
if(ch=='i' || ch=='I') return true;
if(ch=='o' || ch=='O') return true;
if(ch=='u' || ch=='U') return true;
return false;
}
}
output->
enter string=
hey i am ved varshney
total vowels are=6
Comments
Post a Comment