C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Q. Program to count the total number of vowels and consonants in a string.
In this program, our task is to count the total number of vowels and consonants present in the given string. As we know that, The characters a, e, i, o, u are known as vowels in the English alphabet. Any character other than that is known as the consonant. To solve this problem, First of all, we need to convert every upper-case character in the string to lower-case so that the comparisons can be done with the lower-case vowels only not upper-case vowels, i.e.(A, E, I, O, U). Then, we have to traverse the string using a for or while loop and match each character with all the vowels, i.e., a, e, I, o, u. If the match is found, increase the value of count by 1 otherwise continue with the normal flow of the program. The algorithm of the program is given below. Algorithm
ComplexityO(n) SolutionPython
vcount = 0;
ccount = 0;
str = "This is a really simple sentence";
#Converting entire string to lower case to reduce the comparisons
str = str.lower();
for i in range(0,len(str)):
#Checks whether a character is a vowel
if str[i] in ('a',"e","i","o","u"):
vcount = vcount + 1;
elif (str[i] >= 'a' and str[i] <= 'z'):
ccount = ccount + 1;
print("Total number of vowel and consonant are" );
print(vcount);
print(ccount);
Output: Number of vowels: 10 Number of consonants: 17 C
#include <stdio.h>
int main()
{
//Counter variable to store the count of vowels and consonant
int i, vCount = 0, cCount = 0;
char str[] = "This is a really simple sentence";
for(i = 0; i < strlen(str); i++){
str[i] = tolower(str[i]);
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') {
//Increments the vowel counter
vCount++;
}
else if(str[i] >= 'a' && str[i] <= 'z'){
//Increments the consonant counter
cCount++;
}
}
printf("Number of vowels : %d\n", vCount);
printf("Number of consonant : %d", cCount);
return 0;
}
Output: Number of vowels: 10 Number of consonants: 17 JAVA
public class CountVowelConsonant {
public static void main(String[] args) {
//Counter variable to store the count of vowels and consonant
int vCount = 0, cCount = 0;
//Declare a string
String str = "This is a really simple sentence";
//Converting entire string to lower case to reduce the comparisons
str = str.toLowerCase();
for(int i = 0; i < str.length(); i++) {
//Checks whether a character is a vowel
if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
//Increments the vowel counter
vCount++;
}
//Checks whether a character is a consonant
else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') {
//Increments the consonant counter
cCount++;
}
}
System.out.println("Number of vowels: " + vCount);
System.out.println("Number of consonants: " + cCount);
}
}
Output: Number of vowels: 10 Number of consonants: 17 C#
using System;
public class Program
{
public static void Main()
{
//Counter variable to store the count of vowels and consonant
int vCount = 0, cCount = 0;
//Declare a string
string str = "This is a really simple sentence";
//Converting entire string to lower case to reduce the comparisons
str = str.ToLower();
for(int i = 0; i < str.Length; i++) {
//Checks whether a character is a vowel
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') {
//Increments the vowel counter
vCount++;
}
//Checks whether a character is a consonant
else if(str[i] >= 'a' && str[i]<='z') {
//Increments the consonant counter
cCount++;
}
}
Console.WriteLine("Number of vowels : " + vCount);
Console.WriteLine("Number of consonant : " + cCount);
}
}
Output: Number of vowels: 10 Number of consonants: 17 PHP
<!DOCTYPE html>
<html>
<body>
<?php
//Counter variable to store the count of vowels and consonant
$vCount = 0;
$cCount = 0;
//Declare a string
$str = "This is a really simple sentence";
//Converting entire string to lower case to reduce the comparisons
$str = strtolower($str);
for($i = 0; $i < strlen($str); $i++) {
//Checks whether a character is a vowel
if( $str[$i] == 'a' || $str[$i] == 'e' || $str[$i] == 'i' || $str[$i] == 'o' || $str[$i] == 'u') {
//Increments the vowel counter
$vCount++;
}
//Checks whether a character is a consonant
else if($str[$i] >= 'a' && $str[$i] <= 'z') {
//Increments the consonant counter
$cCount++;
}
}
echo "Number of vowels : " , $vCount;
echo "<br>";
echo "\nNumber of consonants : " , $cCount;
?>
</body>
</html>
Output: Number of vowels: 10 Number of consonants: 17
Next TopicPrograms List
|