Home

Count Vowels

Given a string arg, count the vowels and return the number.

Go

package countvowels import "regexp" // CountVowels should have a comment documenting it. func CountVowels(arg string) int { return len(regexp.MustCompile(`[^aeiouAEIOU]`).ReplaceAllString(arg, "")) }

Java

class CountVowels { Integer run(String inputString) { return inputString.replaceAll("[^aeiouAEIOU]", "").length(); } }

JavaScript

const run = (arg) => arg.replace(/[^aeiou]/gi, '').length;

PHP

<?php function countVowels($str) { return strlen(preg_replace("/[^aeiou]/i", "", $str)); }

Python

import re def run(src): return len(re.sub("[^aeiouAEUIO]", "", src))

Ruby

class CountVowels def self.count(input) flt = input.gsub(/[^aeiou]/i, '') return flt.length end end

Rust

extern crate regex; use regex::Regex; pub fn count_vowels(s: String) -> usize { return Regex::new("[^aeiouAEIOU]").unwrap().replace_all(&s, "").len(); }

Repository

https://github.com/okeeffed/developer-notes-nextjs/content/data-structures/count-vowels

Sections


Related