xxxxxxxxxx
// Write a function that returns the number of vowels found within a string.
// Vowel characters are 'a', 'e', 'i', 'o', and 'u'.
// Make sure the function is case insensitive!
// --- Examples
// vowels('What') --> 1
// vowels('Why?') --> 0
// vowels('aEiOu') --> 5
// vowels('I am a world-class developer using iterations') --> 16
function vowels(str) {
const vowel =['a','e' ,'o' , 'u','i']
str= str.toLowerCase()
let count = 0;
for (let index = 0; index < str.length; index++) {
if(vowel.includes(str[index])){
count++
}
}
return count
}
xxxxxxxxxx
# Program: Count number of vowels in a String in Python
example = "Count number of vowels in a String in Python"
# initializing count variable
count = 0
# declaring a variable for index
i = 0
# iterate over the given string (example)
# len(example) -> gives the length of the string in Python
for i in range(len(example)):
if (
(example[i] == "a")
or (example[i] == "e")
or (example[i] == "i")
or (example[i] == "o")
or (example[i] == "u")
):
count += 1
print("Number of vowels in the given string is: ", count)
xxxxxxxxxx
# Python3 Program to count vowels,
# consonant, digits and special
# character in a given string.
# Function to count number of vowels,
# consonant, digits and special
# character in a string.
def countCharacterType(str):
# Declare the variable vowels,
# consonant, digit and special
# characters
vowels = 0
consonant = 0
specialChar = 0
digit = 0
# str.length() function to count
# number of character in given string.
for i in range(0, len(str)):
ch = str[i]
if ( (ch >= 'a' and ch <= 'z') or
(ch >= 'A' and ch <= 'Z') ):
# To handle upper case letters
ch = ch.lower()
if (ch == 'a' or ch == 'e' or ch == 'i'
or ch == 'o' or ch == 'u'):
vowels += 1
else:
consonant += 1
elif (ch >= '0' and ch <= '9'):
digit += 1
else:
specialChar += 1
print("Vowels:", vowels)
print("Consonant:", consonant)
print("Digit:", digit)
print("Special Character:", specialChar)
# Driver function.
str = "geeks for geeks121"
countCharacterType(str)
# This code is contributed by
# Smitha Dinesh Semwal
xxxxxxxxxx
using System.Linq;
public class Program
{
public static int CountVowels(string str)
=> str.Count(a=>$"aeiouAEIOU".Contains(a));
}