Advertisements
Advertisements
Question
Count the total number of vowels in a string.
Code Writing
Advertisements
Solution
import java.util.*;
class clString
{
public void main()
{
Scanner sc = new Scanner(System.in);
String ts = "";
int c = 0, j;
System.out.print("Enter a string: ");
ts = sc.nextLine();
for(j = 0; j<= ts.length() - 1; j++)
{
char ch = ts.charAt(j);
if(ch='a' || ch='e' || ch='i' || ch='o' ||
ch='u' || ch='A' || ch='E' || ch='I' ||
ch='O′ || ch='U' )
{
c++;
}
}
System.out.print(" The number of vowels in " + ts + " is: " + c);
}
}
Output:
Enter the string : Leonardo DA VINCI
The number of vowels in Leonardo DA VINCI is : 7
shaalaa.com
Is there an error in this question or solution?
