In c++
#include<bits/stdc++.h>
using namespace std;
int main()
{
//declaring the char array
char arr[100];
cout<<"Enter the word : ";
//taking the word input
cin>>arr;
int cnst,vow,i;
cnst=vow=i=0;
//traversing the array till it founds the end end alphabet of the word
while(arr[i]!='\0')
{
//checking for vowels
if(arr[i]=='a' || arr[i]=='e' || arr[i]=='i' || arr[i]=='o' || arr[i]=='u' || arr[i]=='y')
{
vow++;
}
//checking for capital letter vowels
else if(arr[i]=='A' || arr[i]=='E' || arr[i]=='I' || arr[i]=='O' || arr[i]=='U' || arr[i]=='Y')
{
vow++;
}
//else counting consonants
else
{
cnst++;
}
i++;
}
cout<<"The word contains "<<vow<<" vowels"<<" and "<<cnst<<" consonants.";
return 0;
}
In Java
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//initializing the Scanner
Scanner sc=new Scanner(System.in);
String word;
int n,i,vow,cnst;
char check;
vow=cnst=0;
System.out.print("Enter the word : ");
//taking the input
word=sc.next();
//calculating the word length
n=word.length();
for(i=0;i<n;i++)
{
check=word.charAt(i);
//checking for vowels
if(check=='a' || check=='e' || check=='i' || check=='o' || check=='u' || check=='y')
{
vow++;
}
//checking for capital letter vowels
else if(check=='A' || check=='E' || check=='I' || check=='O' || check=='U' || check=='Y')
{
vow++;
}
//else counting consonants
else
{
cnst++;
}
}
System.out.println("The word contains "+vow+" vowels and "+cnst+" consonants.");
}
}
Get Answers For Free
Most questions answered within 1 hours.