DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #3 - Vowel Counter

Hope you’re ready for another challenge! Let’s get started with Day 3.

Today’s challenge is modified from user @jayeshcp on CodeWars.

Write a function that returns the number (count) of vowels in a given string. Letters considered as vowels are: a, i, e, o, and u. The function should be able to take all types of characters as input, including lower case letters, upper case letters, symbols, and numbers.

In this challenge, you should be able to efficiently ignore spaces and symbols and discern between capital and lowercase letters. Beginners can start with only lowercase letters and move up from there. It’ll definitely get you ready for tomorrow.

Happy coding!~


Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!

Latest comments (82)

Collapse
 
usenavigate profile image
useNavigate

Ruby :

def get_count(input_str)
 vowels = 'aeiouAEIOU'
 count = 0
  input_str.each_char do |char|
    count += 1 if vowels.include?(char)
  end
  count
end
Enter fullscreen mode Exit fullscreen mode
Collapse
 
zapperdulchen profile image
Zapperdulchen

Clojure

(defn count-vowels [s]
  (let [vowels (set "aouei")]
    (->> (clojure.string/lower-case s)
         (filter vowels)
         count)))

(defn count-vowels-2 [s]
  (count (re-seq #"(?i)[aouei]" s)))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mrdulin profile image
official_dulin

Go:

package kata

import "strings"

func GetCount(str string) (count int) {
  var vowels = "aeiou"
  for _, v := range str {
    if strings.Contains(vowels, string(v)) {
      count++
    }
  }
  return
}
Collapse
 
albakalymuf profile image
mufadhal

function findVowelLetters(s){
var v = ["a","e","o","i","u"];
var vow = "";
var slowerCase = s.toLowerCase();
for(var f of v){
for(var i =0; i<s.length ; i++){
if(slowerCase[i] === f){
vow += f;
}
}
}
return vow;

}

Collapse
 
dimitrilahaye profile image
Dimitri Lahaye
vowels = (str) => (str.match(/[aeiou]/gi) || '').length;

;)

Collapse
 
peter279k profile image
peter279k

Here is my simple solution with PHP:

function getCount($str) {
  $vowelsCount = 0;

  $vowels = ['a', 'e', 'i', 'o', 'u'];

  $index = 0;
  for(; $index < mb_strlen($str); $index++) {
    if (in_array($str[$index], $vowels)) {
      $vowelsCount += 1;
    }
  }

  return $vowelsCount;
}
Collapse
 
kbiedrzycki profile image
Kamil Biedrzycki

Why there's no y? 🤔

Collapse
 
chukkyiii profile image
Jesse Doka • Edited

Python

def vowelcounter(string):
    i = 0
    string = string.lower()

    for x in string:
        if x in 'aeiou':
            i += 1
    print(i)

vowelcounter("This is a test")
Collapse
 
kumsviswam profile image
Kumaravel_Viswam

Python :
def count_vowels(word):
word=word.lower()
data={x:word.count(x) for x in "aeiou"}
return data

Collapse
 
mategreen profile image
matej

Java

Objects.requireNonNull(input, "cannot be null");
return Arrays.stream(input.split(""))
   .map(s -> s.toLowerCase())
   .filter(s -> s.matches("[aeiou]"))
   .count();
Collapse
 
figueroadavid profile image
David Figueroa

Powershell

I do try to keep it as an actual function and give pretty/usable output.

function Get-VowelCount {
    param(
        [string]$InString
    )

    [pscustomobject]@{
        String              = $InString
        LowerCaseCount = [regex]::Matches($InString,'[aeiou]').Count
        UpperCaseCount = [regex]::Matches($InString, '[AEIOU]').count
    }
}
Collapse
 
monicat profile image
Monica Macomber

A little late to the party, but this is my JavaScript solution:

function vowelCounter(text) {
  const letters = text.toLowerCase().split('');
  const count = [...'aeiou'].map(vowel => 
    letters.filter(letter => letter === vowel)).flat().length;
  return count;
}

Collapse
 
centanomics profile image
Cent

My solution is a bit longer because I don't know regex, but it gets the job done.

CodePen


const countVowels = text => {
  let count = 0;
  text.toLowerCase().split("").forEach(char => {
    if (char === "a" || char === "e" || char === "i" || char === "o" || char === "u")
      count++;
  })
  return count;
}

Collapse
 
neotamizhan profile image
Siddharth Venkatesan

Ruby

def vowel_count(text)
  text.scan(/([aeiou])/i).size
end


puts vowel_count("Hello World!!!!") #3
Collapse
 
peterbunting profile image
Peter Bunting

In F#

let isVowel (c:char) =
    match c.ToString().ToLowerInvariant() with
    | "a" | "e" | "i" | "o" | "u" -> 1
    | _ -> 0

let vowelCount (s:string) = s.ToCharArray() |> Array.sumBy(fun x -> isVowel x)

printfn "%d" (vowelCount "The cUnNinG fOx jumpEd over the lazy dog - twice!")