Program Discussion :: Basics
7 / 279
Write a program to to count number of vowels and consonants in a string.
Answer:
import java.io.*;
class q5vowels
{
public static void main(String args[]) throws IOException
{
String str;
int vowels = 0, digits = 0, blanks = 0;
char ch;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String : ");
str = br.readLine();
for(int i = 0; i < str.length(); i++ )
{
ch = str.charAt(i);
if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' ||
ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
vowels++ ;
else if(Character.isDigit(ch))
digits++ ;
else if(Character.isWhitespace(ch))
blanks++ ;
}
System.out.println("Vowels : "+ vowels);
System.out.println("Digits : "+ digits);
System.out.println("Blanks : "+ blanks);
}
}
Asked In ::
Language:

Rai
22 Apr, 2017 11:47 PM
package IBM.String;
import java.util.Scanner;
public class NumberOfVowelsAndConsonants {
public static void main(String args[]){
int vowel=0,consonant=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String");
String s=sc.nextLine();
char arr[]=s.toLowerCase().toCharArray();
for(int i=0;i<arr.length;i++){
char ch=arr[i];
if(Character.isLetter(ch)){
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' ||ch=='u'){
vowel ;
}else{
consonant ;
}
}
}
System.out.print("Number of vowels: "+ vowel +" And Consonat: "+ consonant);
}
}
Language:

Monika
11 Jun, 2017 1:43 PM
<!DOCTYPE html>
<html>
<body>
<?php
function Vowels() {
// input string
$arr ="what is your name";
echo "Input string is :" . $arr."<br/>";
// initialising an empty elements for vowels and consonants
$vowel=0; $consonents=0;
$arrlength=strlen($arr);
// loop for reading the input array
for($x=0; $x< $arrlength; $x++)
{
// comparing each character with vowels
if( ($arr[$x]=="a")||($arr[$x]=="e")||($arr[$x]=="i")||($arr[$x]=="o")||($arr[$x]=="u"))
{
// for repeating the loop and skipping the below statement
$vowel++;
}
else
{
$consonents++;
}
}
// printing vowels
echo "No of vowels is: ". $vowel."<br/>";
echo "No of consonents is: ". $consonents;
}
Vowels();
?>
</body>
</html>
Language:

Ranjan Singh
16 Jun, 2017 12:13 AM
import java.io.*;
class q5vowels
{
public static void main(String args[]) throws IOException
{
String str;
int vowels = 0, digits = 0, blanks = 0;
char ch;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a String : ");
str = br.readLine();
for(int i = 0; i < str.length(); i++ )
{
ch = str.charAt(i);
if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' ||
ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
vowels++ ;
else if(Character.isDigit(ch))
digits++ ;
else if(Character.isWhitespace(ch))
blanks++ ;
}
System.out.println("Vowels : "+ vowels);
System.out.println("Digits : "+ digits);
System.out.println("Blanks : "+ blanks);
}
}
Language:

Kanika
7 Jul, 2017 9:30 AM
#include <stdio.h>
int main()
{
char line[150];
int i, vowels, consonants;
vowels = consonants = 0;
printf("Enter a line of string: ");
scanf("%[^\n]", line);
for(i=0; line[i]!='\0'; ++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
else
{
++consonants;
}
}
printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
return 0;
}
Language:

Neha
7 Jul, 2017 9:30 AM
#include <iostream>
using namespace std;
int main()
{
char line[150];
int vowels, consonants;
vowels = consonants = 0;
cout << "Enter a line of string: ";
cin.getline(line, 150);
for(int i = 0; line[i]!='\0'; ++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
else
{
++consonants;
}
cout << "Vowels: " << vowels << endl;
cout << "Consonants: " << consonants << endl;
return 0;
}
Language:

Veera Chowdary Kamani
7 Sep, 2018 11:13 AM
string=input("enter your sentence\n");v=0;c=0;s=0
vowels=("a","e","i","o","u")
for i in string:
if(i.isalpha()):
if(i in vowels):
v=v 1
else:
c=c 1
elif(i==" "):
s=s 1
print("vowels = " str(v))
print("consonants = " str(c))
print("spaces = " str(s))
if(v==0 or c==0):
print("enter correct sentence")
Language:

Siddhartha Paul
19 Jun, 2019 2:16 PM
import java.io.*;
public class CountVowelConsonant
{
public static void main(String []args)throws IOException
{
int vowel=0,consonant=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string: ");
String str=br.readLine();
int len=str.length();
for(int i=0;i<len;i++)
{
char ch=str.charAt(i); if((ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U')||(ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u'))
vowel++;
else
{
if(ch!=' ')
consonant++;
}
}
System.out.println("No. of vowels: "+vowel+"\nNo. of consonants: "+consonant);
}
}
Language:

Walke
23 Dec, 2021 8:47 AM
Give jv9hg huye lgkj
Language:

Walke
23 Dec, 2021 8:47 AM
Give jv9hg huye lgkj