Department of Information Technology
Department of Information Technology
Experiment No. 3
AIM : Write a java program to count number of vowels, consonants, blank spaces &
Special Characters from the given string .
Program :
import java.util.*;
class ABC123
{
static int i,v=0,c=0,sp=0,n=0;
static char ch;
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("ENTER STRING:");
String s1=s.nextLine();
for(i=0;i<s1.length();i++)
{
ch=s1.charAt(i);
if(ch=='A'||ch=='a'||ch=='E'||ch=='e'||ch=='I'||ch=='i'||ch=='O'||ch=='o'||
ch=='U'||ch=='u')
v++;
else if(ch=='0'||ch=='1'||ch=='2'||ch=='3'||ch=='4'||ch=='5'||ch=='6'||
ch=='7'||ch=='8'||ch=='9')n++;
else if(ch==' ')
sp++;
else
c++;
}
System.out.println("NO. OF VOWELS:"+v);
System.out.println("NO. OF DIGITS:"+n);
System.out.println("NO. OF SPACES:"+sp);
System.out.println("NO. OF CONSONANTS:"+c);
}
}
Output :