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

Content added Content deleted
m (→‎Pascal: remove unused variable)
m (syntax highlighting fixup automation)
Line 11: Line 11:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F isvowel(c)
<syntaxhighlight lang="11l">F isvowel(c)
‘ true if c is an English vowel (ignore y) ’
‘ true if c is an English vowel (ignore y) ’
R c C (‘a’, ‘e’, ‘i’, ‘o’, ‘u’, ‘A’, ‘E’, ‘I’, ‘O’, ‘U’)
R c C (‘a’, ‘e’, ‘i’, ‘o’, ‘u’, ‘A’, ‘E’, ‘I’, ‘O’, ‘U’)
Line 32: Line 32:
V s = ‘Now is the time for all good men to come to the aid of their country.’
V s = ‘Now is the time for all good men to come to the aid of their country.’
V (vcnt, ccnt, vu, cu) = vccounts(s)
V (vcnt, ccnt, vu, cu) = vccounts(s)
print(‘String: ’s"\n Vowels: "vcnt‘ (distinct ’vu")\n Consonants: "ccnt‘ (distinct ’cu‘)’)</lang>
print(‘String: ’s"\n Vowels: "vcnt‘ (distinct ’vu")\n Consonants: "ccnt‘ (distinct ’cu‘)’)</syntaxhighlight>


{{out}}
{{out}}
Line 42: Line 42:


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>PROC CountVovelsConsonants(CHAR ARRAY s BYTE POINTER vov,con)
<syntaxhighlight lang="action!">PROC CountVovelsConsonants(CHAR ARRAY s BYTE POINTER vov,con)
BYTE i
BYTE i
CHAR c
CHAR c
Line 75: Line 75:
Test("Now is the time for all good men to come to the aid of their country.")
Test("Now is the time for all good men to come to the aid of their country.")
Test("Forever Action! programming language")
Test("Forever Action! programming language")
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Count_how_many_vowels_and_consonants_occur_in_a_string.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Count_how_many_vowels_and_consonants_occur_in_a_string.png Screenshot from Atari 8-bit computer]
Line 90: Line 90:
=={{header|Ada}}==
=={{header|Ada}}==
This solution uses Ada 2012 aspect clauses to define discontinuous subtypes
This solution uses Ada 2012 aspect clauses to define discontinuous subtypes
<syntaxhighlight lang="ada">--
<lang Ada>--
-- count vowels and consonants in a string
-- count vowels and consonants in a string
--
--
Line 124: Line 124:
("contains" & vowel_count'Image & " vowels and" & consonant_count'Image &
("contains" & vowel_count'Image & " vowels and" & consonant_count'Image &
" consonants.");
" consonants.");
end count_vowels_and_consonants;</lang>
end count_vowels_and_consonants;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 134: Line 134:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Showing total and distinct vowel/consonant counts, as in the Go, Wren etc. samples.
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 #
<syntaxhighlight 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 #
# returns the 0-based index of the upper case letter c in the alphabet #
# or -1 if c is not a letter #
# or -1 if c is not a letter #
Line 164: Line 164:
print vc counts( "Now is the time for all good men to come to the aid of their country" );
print vc counts( "Now is the time for all good men to come to the aid of their country" );
print vc counts( "Help avoid turns" )
print vc counts( "Help avoid turns" )
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 175: Line 175:
</pre>
</pre>
=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
<lang gwbasic> 100 DIM V(255),C(255)
<syntaxhighlight lang="gwbasic"> 100 DIM V(255),C(255)
110 FOR I = 0 TO 3
110 FOR I = 0 TO 3
120 FOR V = 1 TO 5
120 FOR V = 1 TO 5
Line 212: Line 212:
450 N = N + C( ASC ( MID$ (S$,I,1)))
450 N = N + C( ASC ( MID$ (S$,I,1)))
460 NEXT I
460 NEXT I
470 RETURN</lang>
470 RETURN</syntaxhighlight>
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>vRe: {/[aeiou]/}
<syntaxhighlight lang="rebol">vRe: {/[aeiou]/}
cRe: {/[bcdfghjklmnpqrstvwxyz]/}
cRe: {/[bcdfghjklmnpqrstvwxyz]/}


Line 224: Line 224:


print ["Found" size vowels "vowels -" size unique vowels "unique"]
print ["Found" size vowels "vowels -" size unique vowels "unique"]
print ["Found" size consonants "consonants -" size unique consonants "unique"]</lang>
print ["Found" size consonants "consonants -" size unique consonants "unique"]</syntaxhighlight>


{{out}}
{{out}}
Line 232: Line 232:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>str := "Now is the time for all good men to come to the aid of their country."
<syntaxhighlight lang="autohotkey">str := "Now is the time for all good men to come to the aid of their country."
oV:= [], oC := [], v := c := o := 0
oV:= [], oC := [], v := c := o := 0
for i, ch in StrSplit(str)
for i, ch in StrSplit(str)
Line 252: Line 252:


MsgBox % result := str "`n`n" v+c+o " characters, " v " vowels, " c " consonants and " o " other"
MsgBox % result := str "`n`n" v+c+o " characters, " v " vowels, " c " consonants and " o " other"
. "`n" Vowels "`n" Consonants</lang>
. "`n" Vowels "`n" Consonants</syntaxhighlight>
{{out}}
{{out}}
<pre>Now is the time for all good men to come to the aid of their country.
<pre>Now is the time for all good men to come to the aid of their country.
Line 260: Line 260:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f COUNT_HOW_MANY_VOWELS_AND_CONSONANTS_OCCUR_IN_A_STRING.AWK
# syntax: GAWK -f COUNT_HOW_MANY_VOWELS_AND_CONSONANTS_OCCUR_IN_A_STRING.AWK
BEGIN {
BEGIN {
Line 280: Line 280:
exit(0)
exit(0)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 288: Line 288:


=={{header|BCPL}}==
=={{header|BCPL}}==
<lang bcpl>get "libhdr"
<syntaxhighlight lang="bcpl">get "libhdr"


let ucase(c) =
let ucase(c) =
Line 319: Line 319:


let start() be
let start() be
example("If not now, then when? If not us, then who?")</lang>
example("If not now, then when? If not us, then who?")</syntaxhighlight>
{{out}}
{{out}}
<pre>'If not now, then when? If not us, then who?': 10 vowels, 20 consonants.</pre>
<pre>'If not now, then when? If not us, then who?': 10 vowels, 20 consonants.</pre>


=={{header|C}}==
=={{header|C}}==
<syntaxhighlight lang="c">/*
<lang C>/*


https://rosettacode.org/wiki/Count_how_many_vowels_and_consonants_occur_in_a_string
https://rosettacode.org/wiki/Count_how_many_vowels_and_consonants_occur_in_a_string
Line 400: Line 400:
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 415: Line 415:


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>ucase = proc (c: char) returns (char)
<syntaxhighlight lang="clu">ucase = proc (c: char) returns (char)
if c>='a' & c<='z' then return(char$i2c(char$c2i(c)-32))
if c>='a' & c<='z' then return(char$i2c(char$c2i(c)-32))
else return(c)
else return(c)
Line 455: Line 455:
start_up = proc ()
start_up = proc ()
example("If not now, then when? If not us, then who?")
example("If not now, then when? If not us, then who?")
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre>"If not now, then when? If not us, then who?": 10 vowels, 20 consonants.</pre>
<pre>"If not now, then when? If not us, then who?": 10 vowels, 20 consonants.</pre>


=={{header|COBOL}}==
=={{header|COBOL}}==
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. VOWELS-AND-CONSONANTS.
PROGRAM-ID. VOWELS-AND-CONSONANTS.
Line 513: Line 513:
COUNT-CONSONANT.
COUNT-CONSONANT.
INSPECT IN-STR TALLYING N-CONSONANTS FOR ALL CONSONANTS(C).
INSPECT IN-STR TALLYING N-CONSONANTS FOR ALL CONSONANTS(C).
SET C UP BY 1.</lang>
SET C UP BY 1.</syntaxhighlight>
{{out}}
{{out}}
<pre>If not now, then when? If not us, then who?
<pre>If not now, then when? If not us, then who?
Line 520: Line 520:
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==


<lang lisp>(defun vowel-p (c &optional (vowels "aeiou"))
<syntaxhighlight lang="lisp">(defun vowel-p (c &optional (vowels "aeiou"))
(and (characterp c) (characterp (find c vowels :test #'char-equal))))
(and (characterp c) (characterp (find c vowels :test #'char-equal))))


Line 527: Line 527:


(defun count-consonants (s)
(defun count-consonants (s)
(and (stringp s) (- (count-if #'alpha-char-p s) (count-vowels s))))</lang>
(and (stringp s) (- (count-if #'alpha-char-p s) (count-vowels s))))</syntaxhighlight>


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";


sub vowels_consonants(s: [uint8]): (vowels: intptr, consonants: intptr) is
sub vowels_consonants(s: [uint8]): (vowels: intptr, consonants: intptr) is
Line 565: Line 565:
end sub;
end sub;


example("If not now, then when? If not us, then who?");</lang>
example("If not now, then when? If not us, then who?");</syntaxhighlight>
{{out}}
{{out}}
<pre>'If not now, then when? If not us, then who?': 10 vowels, 20 consonants.</pre>
<pre>'If not now, then when? If not us, then who?': 10 vowels, 20 consonants.</pre>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Count how many vowels and consonants occur in a string. Nigel Galloway: August 1th., 202
// Count how many vowels and consonants occur in a string. Nigel Galloway: August 1th., 202
type cType = Vowel |Consonant |Other
type cType = Vowel |Consonant |Other
Line 576: Line 576:
let n="Now is the time for all good men to come to the aid of their country."|>Seq.countBy(System.Char.ToLower>>fN)
let n="Now is the time for all good men to come to the aid of their country."|>Seq.countBy(System.Char.ToLower>>fN)
printfn "%A" n
printfn "%A" n
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 584: Line 584:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: ascii combinators io kernel math.statistics prettyprint
<syntaxhighlight lang="factor">USING: ascii combinators io kernel math.statistics prettyprint
sequences ;
sequences ;


Line 596: Line 596:
"Forever Factor programming language"
"Forever Factor programming language"
"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."
[ dup ... " -> " write [ letter-type ] histogram-by . nl ] bi@</lang>
[ dup ... " -> " write [ letter-type ] histogram-by . nl ] bi@</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 608: Line 608:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>
<syntaxhighlight lang="freebasic">
Dim As String cadena = """Forever the FreeBASIC programming language"""
Dim As String cadena = """Forever the FreeBASIC programming language"""
Dim As Integer vocal = 0, consonante = 0
Dim As Integer vocal = 0, consonante = 0
Line 642: Line 642:
Print "In string occur"; vocal; " vowels"
Print "In string occur"; vocal; " vowels"
Print "In string occur"; consonante; " consonants"
Print "In string occur"; consonante; " consonants"
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 653: Line 653:
=={{header|Go}}==
=={{header|Go}}==
Same approach as the Wren entry.
Same approach as the Wren entry.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 687: Line 687:
fmt.Printf("contains (distinct %d vowels and %d consonants.\n\n", len(vmap), len(cmap))
fmt.Printf("contains (distinct %d vowels and %d consonants.\n\n", len(vmap), len(cmap))
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 705: Line 705:
One of (at least) four possible meanings here:
One of (at least) four possible meanings here:


<lang haskell>import Control.Monad (join)
<syntaxhighlight lang="haskell">import Control.Monad (join)
import Data.Bifunctor (bimap, first, second)
import Data.Bifunctor (bimap, first, second)
import Data.Bool (bool)
import Data.Bool (bool)
Line 756: Line 756:
if p
if p
then t
then t
else f</lang>
else f</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Unique vowels and consonants used, with counts:
<pre>Unique vowels and consonants used, with counts:
Line 766: Line 766:
Another of (at least) four possible meanings:
Another of (at least) four possible meanings:


<lang haskell>import Control.Monad (join)
<syntaxhighlight lang="haskell">import Control.Monad (join)
import Data.Bifunctor (bimap)
import Data.Bifunctor (bimap)
import Data.Char (isAlpha)
import Data.Char (isAlpha)
Line 835: Line 835:


both :: (a -> b) -> (a, a) -> (b, b)
both :: (a -> b) -> (a, a) -> (b, b)
both = join bimap</lang>
both = join bimap</syntaxhighlight>
{{Out}}
{{Out}}
<pre>33 'vowels and consonants'
<pre>33 'vowels and consonants'
Line 863: Line 863:
Implementation (two tallies: vowels first, consonants second):
Implementation (two tallies: vowels first, consonants second):


<lang j>vowel=: (,toupper) 'aeiou'
<syntaxhighlight lang="j">vowel=: (,toupper) 'aeiou'
consonant=: (,toupper) (a.{~97+i.16) -. vowel
consonant=: (,toupper) (a.{~97+i.16) -. vowel
vctally=: e.&vowel ,&(+/) e.&consonant</lang>
vctally=: e.&vowel ,&(+/) e.&consonant</syntaxhighlight>


Examples:
Examples:


<lang J> vctally 'Now is the time for all good men to come to the aid of their country.'
<syntaxhighlight lang="j"> vctally 'Now is the time for all good men to come to the aid of their country.'
22 18
22 18
vctally 'Forever Action! programming language'
vctally 'Forever Action! programming language'
13 13</lang>
13 13</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
Line 884: Line 884:


===Count of "Vowels and Consonants" ?===
===Count of "Vowels and Consonants" ?===
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
"use strict";
"use strict";


Line 910: Line 910:
// MAIN ---
// MAIN ---
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>33 "vowels and consonants"</pre>
<pre>33 "vowels and consonants"</pre>


===Counts of distinct vowels and distinct consonants seen ?===
===Counts of distinct vowels and distinct consonants seen ?===
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
"use strict";
"use strict";


Line 999: Line 999:
// MAIN ---
// MAIN ---
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Distinct vowels: (aeiou, 5)
<pre>Distinct vowels: (aeiou, 5)
Line 1,006: Line 1,006:


===Counts of vowel and consonant occurrences ?===
===Counts of vowel and consonant occurrences ?===
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
"use strict";
"use strict";


Line 1,092: Line 1,092:


return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Vowel occurrences: 12
<pre>Vowel occurrences: 12
Line 1,099: Line 1,099:


===Counts of occurrence for each vowel and consonant ?===
===Counts of occurrence for each vowel and consonant ?===
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
"use strict";
"use strict";


Line 1,236: Line 1,236:
// MAIN ---
// MAIN ---
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Vowel counts:
<pre>Vowel counts:
Line 1,265: Line 1,265:


This entry focuses solely on the A-Z alphabet.
This entry focuses solely on the A-Z alphabet.
<syntaxhighlight lang="jq">
<lang jq>
def is_lowercase_vowel: IN("a","e","i","o","u");
def is_lowercase_vowel: IN("a","e","i","o","u");
def is_lowercase_letter: "a" <= . and . <= "z";
def is_lowercase_letter: "a" <= . and . <= "z";
Line 1,290: Line 1,290:
| pp, "";
| pp, "";


task</lang>
task</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,314: Line 1,314:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>isvowel(c) = c in ['a', 'e', 'i', 'o', 'u', 'A', 'E', "I", 'O', 'U']
<syntaxhighlight lang="julia">isvowel(c) = c in ['a', 'e', 'i', 'o', 'u', 'A', 'E', "I", 'O', 'U']
isletter(c) = 'a' <= c <= 'z' || 'A' <= c <= 'Z'
isletter(c) = 'a' <= c <= 'z' || 'A' <= c <= 'Z'
isconsonant(c) = !isvowel(c) && isletter(c)
isconsonant(c) = !isvowel(c) && isletter(c)
Line 1,335: Line 1,335:


testvccount()
testvccount()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
String: Forever Julia programming language
String: Forever Julia programming language
Line 1,347: Line 1,347:


=={{header|Ksh}}==
=={{header|Ksh}}==
<lang ksh>
<syntaxhighlight lang="ksh">
#!/bin/ksh
#!/bin/ksh


Line 1,405: Line 1,405:
printf "Consonants: %3d (Unique: %2d)\n" "${lettercnt[0]}" "${uniquecnt[0]}"
printf "Consonants: %3d (Unique: %2d)\n" "${lettercnt[0]}" "${uniquecnt[0]}"
printf " Vowlels: %3d (Unique: %2d)\n" "${lettercnt[1]}" "${uniquecnt[1]}"
printf " Vowlels: %3d (Unique: %2d)\n" "${lettercnt[1]}" "${uniquecnt[1]}"
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
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.
Line 1,413: Line 1,413:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>vowels = {"a", "e", "i", "o", "u"};
<syntaxhighlight lang="mathematica">vowels = {"a", "e", "i", "o", "u"};
conso = {"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"};
conso = {"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z"};
vowels = Join[vowels, ToUpperCase@vowels];
vowels = Join[vowels, ToUpperCase@vowels];
Line 1,420: Line 1,420:
<|"vowels" -> StringCount[str, Alternatives @@ vowels],
<|"vowels" -> StringCount[str, Alternatives @@ vowels],
"consonants" -> StringCount[str, Alternatives @@ conso],
"consonants" -> StringCount[str, Alternatives @@ conso],
"other" -> StringCount[str, Except[Alternatives @@ Join[vowels, conso]]]|></lang>
"other" -> StringCount[str, Except[Alternatives @@ Join[vowels, conso]]]|></syntaxhighlight>
{{out}}
{{out}}
<pre><|"vowels" -> 22, "consonants" -> 24, "other" -> 11|></pre>
<pre><|"vowels" -> 22, "consonants" -> 24, "other" -> 11|></pre>


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<lang modula2>MODULE VowelsAndConsonants;
<syntaxhighlight lang="modula2">MODULE VowelsAndConsonants;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;
FROM Strings IMPORT Length;
FROM Strings IMPORT Length;
Line 1,478: Line 1,478:
BEGIN
BEGIN
Display("If not now, then when? If not us, then who?");
Display("If not now, then when? If not us, then who?");
END VowelsAndConsonants.</lang>
END VowelsAndConsonants.</syntaxhighlight>
{{out}}
{{out}}
<pre>"If not now, then when? If not us, then who?": 10 vowels, 20 consonants.</pre>
<pre>"If not now, then when? If not us, then who?": 10 vowels, 20 consonants.</pre>


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import strutils
<syntaxhighlight lang="nim">import strutils


const
const
Line 1,508: Line 1,508:
value(consonantCount, "consonant"))
value(consonantCount, "consonant"))


vcCount("Now is the time for all good men to come to the aid of their country.")</lang>
vcCount("Now is the time for all good men to come to the aid of their country.")</syntaxhighlight>


{{out}}
{{out}}
Line 1,517: Line 1,517:
=={{header|Pascal}}==
=={{header|Pascal}}==
Standard “Unextended” Pascal (ISO standard 7185) does not really know the notion of strings:
Standard “Unextended” Pascal (ISO standard 7185) does not really know the notion of strings:
<lang pascal>program countHowManyVowelsAndConsonantsOccurInAString(input, output);
<syntaxhighlight lang="pascal">program countHowManyVowelsAndConsonantsOccurInAString(input, output);


var
var
Line 1,546: Line 1,546:
writeLn(vowelCount, ' vowels');
writeLn(vowelCount, ' vowels');
writeLn(consonantCount, ' consonants')
writeLn(consonantCount, ' consonants')
end.</lang>
end.</syntaxhighlight>
{{in}}
{{in}}
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
Line 1,554: Line 1,554:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Count_how_many_vowels_and_consonants_occur_in_a_string
use strict; # https://rosettacode.org/wiki/Count_how_many_vowels_and_consonants_occur_in_a_string
Line 1,569: Line 1,569:
TEST ONE
TEST ONE
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.
Forever Perl Programming Language</lang>
Forever Perl Programming Language</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,584: Line 1,584:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">count_vowels_and_consonants</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">count_vowels_and_consonants</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Line 1,596: Line 1,596:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">count_vowels_and_consonants</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Now is the time for all good men to come to the aid of their country."</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">count_vowels_and_consonants</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Now is the time for all good men to come to the aid of their country."</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,606: Line 1,606:
===List comprehension===
===List comprehension===
Also using maps for counting individual characters.
Also using maps for counting individual characters.
<lang Picat>main =>
<syntaxhighlight lang="picat">main =>
S = "Count how many vowels and consonants occur in a string",
S = "Count how many vowels and consonants occur in a string",
vowels(Vowels),
vowels(Vowels),
Line 1,630: Line 1,630:
foreach(C in S, (Cs != "" -> membchk(C,Cs) ; true))
foreach(C in S, (Cs != "" -> membchk(C,Cs) ; true))
Map.put(C,Map.get(C,0)+1)
Map.put(C,Map.get(C,0)+1)
end.</lang>
end.</syntaxhighlight>


{{out}}
{{out}}
Line 1,640: Line 1,640:


===Recursion===
===Recursion===
<lang Picat>main =>
<syntaxhighlight lang="picat">main =>
S = "Count how many vowels and consonants occur in a string",
S = "Count how many vowels and consonants occur in a string",
vowels(Vowels),
vowels(Vowels),
Line 1,662: Line 1,662:
Vs1 = Vs0
Vs1 = Vs0
),
),
count_set(Set,Cs,Vs1,Vs).</lang>
count_set(Set,Cs,Vs1,Vs).</syntaxhighlight>


{{out}}
{{out}}
Line 1,670: Line 1,670:
=={{header|Python}}==
=={{header|Python}}==
{{trans|Julia}}
{{trans|Julia}}
<lang python>def isvowel(c):
<syntaxhighlight lang="python">def isvowel(c):
""" true if c is an English vowel (ignore y) """
""" true if c is an English vowel (ignore y) """
return c in ['a', 'e', 'i', 'o', 'u', 'A', 'E', "I", 'O', 'U']
return c in ['a', 'e', 'i', 'o', 'u', 'A', 'E', "I", 'O', 'U']
Line 1,698: Line 1,698:


testvccount()
testvccount()
</lang>{{out}}<pre>
</syntaxhighlight>{{out}}<pre>
String: Forever Python programming language
String: Forever Python programming language
Vowels: 11 (distinct 5)
Vowels: 11 (distinct 5)
Line 1,710: Line 1,710:


Or, selecting another of the various possible meanings of an ambiguous task description:
Or, selecting another of the various possible meanings of an ambiguous task description:
<lang python>'''Total and individual counts of vowel and consonant instances'''
<syntaxhighlight lang="python">'''Total and individual counts of vowel and consonant instances'''


from functools import reduce
from functools import reduce
Line 1,806: Line 1,806:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>33 "vowels and consonants"
<pre>33 "vowels and consonants"
Line 1,830: Line 1,830:
=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery> [ bit
<syntaxhighlight lang="quackery"> [ bit
[ 0 $ "AEIOUaeiuo"
[ 0 $ "AEIOUaeiuo"
witheach [ bit | ] ] constant
witheach [ bit | ] ] constant
Line 1,851: Line 1,851:
echo say " vowels" cr
echo say " vowels" cr
echo say " consonants"</lang>
echo say " consonants"</syntaxhighlight>


{{out}}
{{out}}
Line 1,861: Line 1,861:
'''OR''', depending on how you interpret the task…
'''OR''', depending on how you interpret the task…


<lang Quackery> [ 0 $ "AEIOU"
<syntaxhighlight lang="quackery"> [ 0 $ "AEIOU"
witheach [ bit | ] ] constant is vowels ( --> n )
witheach [ bit | ] ] constant is vowels ( --> n )


Line 1,883: Line 1,883:
echo say " distinct vowels" cr
echo say " distinct vowels" cr
echo say " distinct consonants"</lang>
echo say " distinct consonants"</syntaxhighlight>


{{out}}
{{out}}
Line 1,894: Line 1,894:
Note that the task '''does not''' ask for the '''total count''' of vowels and consonants, but for '''how many''' occur.
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>;
<syntaxhighlight lang="raku" line>my @vowels = <a e i o u>;
my @consonants = <b c d f g h j k l m n p q r s t v w x y z>;
my @consonants = <b c d f g h j k l m n p q r s t v w x y z>;


Line 1,902: Line 1,902:
}
}


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


{{out}}
{{out}}
Line 1,909: Line 1,909:
=={{header|REXX}}==
=={{header|REXX}}==
=== version 1 ===
=== version 1 ===
<lang rexx>/* REXX */
<syntaxhighlight lang="rexx">/* REXX */
Parse Arg s
Parse Arg s
If s='' Then
If s='' Then
Line 1,931: Line 1,931:
sv=translate(s,copies('+',length(vow))copies(' ',256),vow||xrange('00'x,'ff'x))
sv=translate(s,copies('+',length(vow))copies(' ',256),vow||xrange('00'x,'ff'x))
Say length(space(sc,0)) tag 'consonants,' length(space(sv,0)) tag 'vowels'
Say length(space(sc,0)) tag 'consonants,' length(space(sv,0)) tag 'vowels'
Return</lang>
Return</syntaxhighlight>
{{out}}
{{out}}
<pre>Forever Wren programming language
<pre>Forever Wren programming language
Line 1,938: Line 1,938:


=== version 2 ===
=== version 2 ===
<lang rexx>/*REXX program counts the vowels and consonants (unique and total) in a given string. */
<syntaxhighlight lang="rexx">/*REXX program counts the vowels and consonants (unique and total) in a given string. */
parse arg $ /*obtain optional argument from the CL.*/
parse arg $ /*obtain optional argument from the CL.*/
if $='' then $= 'Now is the time for all good men to come to the aid of their country.'
if $='' then $= 'Now is the time for all good men to come to the aid of their country.'
Line 1,950: Line 1,950:
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
cnt: arg k; do j=1 to length(@.k); if pos(substr(@.k,j,1),$)>0 then #.k=#.k+1; end; return
cnt: arg k; do j=1 to length(@.k); if pos(substr(@.k,j,1),$)>0 then #.k=#.k+1; end; return
init: @.1='AEIOU'; @.2="BCDFGHJKLMNPQRSTVWXYZ"; upper $; $=space($,0); L=length($); return</lang>
init: @.1='AEIOU'; @.2="BCDFGHJKLMNPQRSTVWXYZ"; upper $; $=space($,0); L=length($); return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 1,959: Line 1,959:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>? "working..."
<syntaxhighlight lang="ring">? "working..."
str = '"' + "Forever Ring Programming Language" + '"'
str = '"' + "Forever Ring Programming Language" + '"'
vowel = 0 vowels = [] for x in "AEIOUaeiou" add(vowels, x) next
vowel = 0 vowels = [] for x in "AEIOUaeiou" add(vowels, x) next
Line 1,972: Line 1,972:
? "In string occur " + vowel + " vowels"
? "In string occur " + vowel + " vowels"
? "In string occur " + (ltrc - vowel) + " consonants"
? "In string occur " + (ltrc - vowel) + " consonants"
put "done..."</lang>
put "done..."</syntaxhighlight>
{{out}}
{{out}}
<pre>working...
<pre>working...
Line 1,981: Line 1,981:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>RE_V = /[aeiou]/
<syntaxhighlight lang="ruby">RE_V = /[aeiou]/
RE_C = /[bcdfghjklmnpqrstvwxyz]/
RE_C = /[bcdfghjklmnpqrstvwxyz]/
str = "Now is the time for all good men to come to the aid of their country."
str = "Now is the time for all good men to come to the aid of their country."
Line 1,994: Line 1,994:


grouped.each{|k,v| puts "#{k}: #{v.size}, #{v.uniq.size} unique."}
grouped.each{|k,v| puts "#{k}: #{v.size}, #{v.uniq.size} unique."}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>Consonants: 31, 13 unique.
<pre>Consonants: 31, 13 unique.
Line 2,003: Line 2,003:
=={{header|SNOBOL4}}==
=={{header|SNOBOL4}}==
{{works with|SNOBOL4, SPITBOL for Linux}}
{{works with|SNOBOL4, SPITBOL for Linux}}
<syntaxhighlight lang="snobol4">
<lang SNOBOL4>
* Program: countvc.sbl,
* Program: countvc.sbl,
* To run: sbl countvc.sbl
* To run: sbl countvc.sbl
Line 2,093: Line 2,093:


END
END
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,122: Line 2,122:
{{libheader|Wren-str}}
{{libheader|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.
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
<syntaxhighlight lang="ecmascript">import "/str" for Str


var vowels = "aeiou"
var vowels = "aeiou"
Line 2,150: Line 2,150:
System.print("contains (total) %(vc) vowels and %(cc) consonants.")
System.print("contains (total) %(vc) vowels and %(cc) consonants.")
System.print("contains (distinct) %(vmap.count) vowels and %(cmap.count) consonants.\n")
System.print("contains (distinct) %(vmap.count) vowels and %(cmap.count) consonants.\n")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,165: Line 2,165:
=={{header|X86 Assembly}}==
=={{header|X86 Assembly}}==
Translation of XPL0. Assemble with tasm, tlink /t
Translation of XPL0. Assemble with tasm, tlink /t
<lang asm> .model tiny
<syntaxhighlight lang="asm"> .model tiny
.code
.code
.486
.486
Line 2,295: Line 2,295:
msg4 db " consonants."
msg4 db " consonants."
crlf db 0Dh, 0Ah, 0
crlf db 0Dh, 0Ah, 0
end start</lang>
end start</syntaxhighlight>


{{out}}
{{out}}
Line 2,309: Line 2,309:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>string 0; \use zero-terminated strings
<syntaxhighlight lang="xpl0">string 0; \use zero-terminated strings
int VTC, VDC, \vowel total count, vowel distinct count
int VTC, VDC, \vowel total count, vowel distinct count
CTC, CDC, \consonant total count, consonant distinct count
CTC, CDC, \consonant total count, consonant distinct count
Line 2,344: Line 2,344:
CrLf(0);
CrLf(0);
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}