Count how many vowels and consonants occur in a string

From Rosetta Code
Revision as of 14:32, 26 July 2021 by PureFox (talk | contribs) (→‎{{header|Wren}}: Rewritten to show distinct as well as total numbers of vowels and consonants.)
Count how many vowels and consonants occur in a string is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task
Check how many vowels and consonants occur in a string



Go

Same approach as the Wren entry. <lang go>package main

import (

   "fmt"
   "strings"

)

func main() {

   const (
       vowels     = "aeiou"
       consonants = "bcdfghjklmnpqrstvwxyz"
   )
   strs := []string{
       "Forever Go programming language",
       "Now is the time for all good men to come to the aid of their country.",
   }
   for _, str := range strs {
       fmt.Println(str)
       str = strings.ToLower(str)
       vc, cc := 0, 0
       for _, c := range str {
           if strings.ContainsRune(vowels, c) {
               vc++
           } else if strings.ContainsRune(consonants, c) {
               cc++
           }
       }
       fmt.Printf("contains %d vowels and %d consonants.\n\n", vc, cc)
   }

}</lang>

Output:
Forever Go programming language
contains 11 vowels and 17 consonants.

Now is the time for all good men to come to the aid of their country.
contains 22 vowels and 31 consonants.

Raku

Note that the task does not ask for the total count of vowels and consonants, but for how many occur.

<lang perl6>my @vowels = <a e i o u>; my @consonants = ;

sub letter-check ($string) {

   my $letters = $string.lc.comb.Set;
   "{sum $letters{@vowels}} vowels and {sum $letters{@consonants}} consonants occur in the string \"$string\"";

}

say letter-check "Forever Ring Programming Language";</lang>

Output:
5 vowels and 8 consonants occur in the string "Forever Ring Programming Language"


Ring

This example is incorrect. Please fix the code and remove this message.

Details: [, ], \, _, and ` are not consonants

<lang ring> load "stdlib.ring" see "working..." + nl str = '"' + "Forever Ring Programming Language" + '"' vowel = 0 cons =0

for n = 1 to len(str)

   if isvowel(str[n]) = 1
      vowel += 1
   else not isvowel(str[n]) and ascii(str[n]) > 64 and ascii(str[n]) < 123
      cons += 1
   ok

next

see "Input string = " + str + nl see "In string occur " + vowel + " vowels" + nl see "In string occur " + cons + " consonants" + nl see "done..." + nl </lang>

Output:
working...
Input string = "Forever Ring Programming Language"
In string occur 11 vowels
In string occur 24 consonants
done...

Wren

Library: Wren-str

In the absence of any indications to the contrary, we take a simplistic view of only considering English ASCII vowels (not 'y') and consonants. <lang ecmascript>import "/str" for Str

var vowels = "aeiou" var consonants = "bcdfghjklmnpqrstvwxyz"

var strs = [

   "Forever Wren programming language",
   "Now is the time for all good men to come to the aid of their country."

]

for (str in strs) {

   System.print(str)
   str = Str.lower(str)
   var vc = 0
   var cc = 0
   var vmap = {}
   var cmap = {}
   for (c in str) {
       if (vowels.contains(c)) {
           vc = vc  + 1
           vmap[c] = true
       } else if (consonants.contains(c)) {
           cc = cc + 1
           cmap[c] = true
       }
   }
   System.print("contains (total) %(vc) vowels and %(cc) consonants.")
   System.print("contains (distinct) %(vmap.count) vowels and %(cmap.count) consonants.\n")

}</lang>

Output:
Forever Wren programming language
contains (total) 11 vowels and 19 consonants.
contains (distinct) 5 vowels and 9 consonants.

Now is the time for all good men to come to the aid of their country.
contains (total) 22 vowels and 31 consonants.
contains (distinct) 5 vowels and 13 consonants.