Count number of vowel in a given string in Java.
import java.util.*;
public class code {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter string=");
String s=sc.nextLine();
int vow=0;
for(int i=0; i<s.length(); i++){
char ch=s.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
vow++;
}
System.out.println("total vowel in string are="+vow);
}
}
// method 2->
import java.util.*;
public class code {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter string=");
String s=sc.nextLine();
int vow=0;
for(int i=0; i<s.length(); i++){
if(isvowel(s.charAt(i))==true)
vow++;
}
System.out.println("total vowel in string are="+vow);
}
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;
}
}
Comments
Post a Comment