Count how many vowels and consonants occur in a string: Difference between revisions

Content added Content deleted
(→‎{{header|Wren}}: Rewritten to show distinct as well as total numbers of vowels and consonants.)
(→‎{{header|Go}}: Similar changes to Wren entry.)
Line 27: Line 27:
str = strings.ToLower(str)
str = strings.ToLower(str)
vc, cc := 0, 0
vc, cc := 0, 0
vmap := make(map[rune]bool)
cmap := make(map[rune]bool)
for _, c := range str {
for _, c := range str {
if strings.ContainsRune(vowels, c) {
if strings.ContainsRune(vowels, c) {
vc++
vc++
vmap[c] = true
} else if strings.ContainsRune(consonants, c) {
} else if strings.ContainsRune(consonants, c) {
cc++
cc++
cmap[c] = true
}
}
}
}
fmt.Printf("contains %d vowels and %d consonants.\n\n", vc, cc)
fmt.Printf("contains (total) %d vowels and %d consonants.\n", vc, cc)
fmt.Printf("contains (distinct %d vowels and %d consonants.\n\n", len(vmap), len(cmap))
}
}
}</lang>
}</lang>
Line 41: Line 46:
<pre>
<pre>
Forever Go programming language
Forever Go programming language
contains 11 vowels and 17 consonants.
contains (total) 11 vowels and 17 consonants.
contains (distinct 5 vowels and 8 consonants.


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