Count how many vowels and consonants occur in a string: Difference between revisions
→{{header|Factor}}: simplify |
Added Algol 68 |
||
Line 4:
<br><br>
=={{header|ALGOL 68}}==
Showing total and distinct vowel/consonant counts, as in the Go, Wren etc. samples.
<lang algol68>BEGIN # count the vowels and consonants in a string #
# returns the 0-based index of the upper case letter c in the alphabet #
# or -1 if c is not a letter #
OP L = ( CHAR c )INT:
IF c >= "A" AND c <= "Z" THEN ABS c - ABS "A" ELIF c >= "a" AND c <= "z" THEN ABS c - ABS "a" ELSE -1 FI;
# prints the counts of vowels and consonants in s #
PROC print vc counts = ( STRING s )VOID:
BEGIN
[ 0 : 26 ]BOOL used; FOR i TO UPB used DO used[ i ] := FALSE OD;
[ 0 : 26 ]BOOL vowel; FOR i TO UPB vowel DO vowel[ i ] := FALSE OD;
vowel[ L "A" ] := vowel[ L "E" ] := vowel[ L "I" ] := vowel[ L "O" ] := vowel[ L "U" ] := TRUE;
INT v total := 0, c total := 0;
FOR i FROM LWB s TO UPB s DO
IF INT c index = L s[ i ];
c index >= LWB used
THEN used[ c index ] := TRUE;
IF vowel[ c index ] THEN v total ELSE c total FI +:= 1
FI
OD;
INT v count := 0, c count := 0;
FOR i FROM LWB used TO UPB used DO
IF used[ i ] THEN IF vowel[ i ] THEN v count ELSE c count FI +:= 1 FI
OD;
print( ( """", s, """ contains", newline ) );
print( ( " ", whole( v count, 0 ), " vowels and ", whole( c count, 0 ), " consonants (distinct)", newline ) );
print( ( " ", whole( v total, 0 ), " vowels and ", whole( c total, 0 ), " consonants (total)", newline ) )
END; # print vc counts #
# test cases #
print vc counts( "Now is the time for all good men to come to the aid of their country" );
print vc counts( "Help avoid rust" )
END</lang>
{{out}}
<pre>
"Now is the time for all good men to come to the aid of their country" contains
5 vowels and 13 consonants (distinct)
22 vowels and 31 consonants (total)
"Help avoid rust" contains
5 vowels and 8 consonants (distinct)
5 vowels and 8 consonants (total)
</pre>
=={{header|Factor}}==
|
Revision as of 18:36, 26 July 2021
- Task
- Count how many vowels and consonants occur in a string
ALGOL 68
Showing total and distinct vowel/consonant counts, as in the Go, Wren etc. samples. <lang algol68>BEGIN # count the vowels and consonants in a string #
# returns the 0-based index of the upper case letter c in the alphabet # # or -1 if c is not a letter # OP L = ( CHAR c )INT: IF c >= "A" AND c <= "Z" THEN ABS c - ABS "A" ELIF c >= "a" AND c <= "z" THEN ABS c - ABS "a" ELSE -1 FI; # prints the counts of vowels and consonants in s # PROC print vc counts = ( STRING s )VOID: BEGIN [ 0 : 26 ]BOOL used; FOR i TO UPB used DO used[ i ] := FALSE OD; [ 0 : 26 ]BOOL vowel; FOR i TO UPB vowel DO vowel[ i ] := FALSE OD; vowel[ L "A" ] := vowel[ L "E" ] := vowel[ L "I" ] := vowel[ L "O" ] := vowel[ L "U" ] := TRUE; INT v total := 0, c total := 0; FOR i FROM LWB s TO UPB s DO IF INT c index = L s[ i ]; c index >= LWB used THEN used[ c index ] := TRUE; IF vowel[ c index ] THEN v total ELSE c total FI +:= 1 FI OD; INT v count := 0, c count := 0; FOR i FROM LWB used TO UPB used DO IF used[ i ] THEN IF vowel[ i ] THEN v count ELSE c count FI +:= 1 FI OD; print( ( """", s, """ contains", newline ) ); print( ( " ", whole( v count, 0 ), " vowels and ", whole( c count, 0 ), " consonants (distinct)", newline ) ); print( ( " ", whole( v total, 0 ), " vowels and ", whole( c total, 0 ), " consonants (total)", newline ) ) END; # print vc counts # # test cases # print vc counts( "Now is the time for all good men to come to the aid of their country" ); print vc counts( "Help avoid rust" )
END</lang>
- Output:
"Now is the time for all good men to come to the aid of their country" contains 5 vowels and 13 consonants (distinct) 22 vowels and 31 consonants (total) "Help avoid rust" contains 5 vowels and 8 consonants (distinct) 5 vowels and 8 consonants (total)
Factor
<lang factor>USING: ascii combinators io kernel math.statistics prettyprint sequences ;
- letter-type ( char -- str )
{ { [ dup "aeiouAEIOU" member? ] [ drop "vowel" ] } { [ Letter? ] [ "consonant" ] } [ "other" ] } cond ;
"Forever Factor programming language" "Now is the time for all good men to come to the aid of their country." [ dup ... " -> " write [ letter-type ] histogram-by . nl ] bi@</lang>
- Output:
"Forever Factor programming language" -> H{ { "other" 3 } { "consonant" 20 } { "vowel" 12 } } "Now is the time for all good men to come to the aid of their country." -> H{ { "other" 16 } { "consonant" 31 } { "vowel" 22 } }
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 vmap := make(map[rune]bool) cmap := make(map[rune]bool) for _, c := range str { if strings.ContainsRune(vowels, c) { vc++ vmap[c] = true } else if strings.ContainsRune(consonants, c) { cc++ cmap[c] = true } } 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>
- Output:
Forever Go programming language 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. contains (total) 22 vowels and 31 consonants. contains (distinct 5 vowels and 13 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
<lang ring> load "stdlib.ring" see "working..." + nl str = '"' + "Forever Ring Programming Language" + '"' vowel = 0 cons =0
for n = 1 to len(str)
strc = str[n] if isvowel(str[n]) = 1 vowel += 1 ok if isconsonant(strc) 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
func isconsonant(c)
bool1 = not isvowel(c) bool2 = (ascii(c) > 64 and ascii(c) < 91) bool3 = (ascii(c) > 96 and ascii(c) < 123) if bool1 and (bool2 or bool3) return 1 else return 0 ok
</lang>
- Output:
working... Input string = "Forever Ring Programming Language" In string occur 11 vowels In string occur 19 consonants done...
Wren
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.