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

m
syntax highlighting fixup automation
m (→‎Pascal: remove unused variable)
m (syntax highlighting fixup automation)
Line 11:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F isvowel(c)
‘ true if c is an English vowel (ignore y) ’
R c C (‘a’, ‘e’, ‘i’, ‘o’, ‘u’, ‘A’, ‘E’, ‘I’, ‘O’, ‘U’)
Line 32:
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)
print(‘String: ’s"\n Vowels: "vcnt‘ (distinct ’vu")\n Consonants: "ccnt‘ (distinct ’cu‘)’)</langsyntaxhighlight>
 
{{out}}
Line 42:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC CountVovelsConsonants(CHAR ARRAY s BYTE POINTER vov,con)
BYTE i
CHAR c
Line 75:
Test("Now is the time for all good men to come to the aid of their country.")
Test("Forever Action! programming language")
RETURN</langsyntaxhighlight>
{{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]
Line 90:
=={{header|Ada}}==
This solution uses Ada 2012 aspect clauses to define discontinuous subtypes
<syntaxhighlight lang="ada">--
<lang Ada>--
-- count vowels and consonants in a string
--
Line 124:
("contains" & vowel_count'Image & " vowels and" & consonant_count'Image &
" consonants.");
end count_vowels_and_consonants;</langsyntaxhighlight>
{{out}}
<pre>
Line 134:
=={{header|ALGOL 68}}==
Showing total and distinct vowel/consonant counts, as in the Go, Wren etc. samples.
<langsyntaxhighlight 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 #
Line 164:
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" )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 175:
</pre>
=={{header|Applesoft BASIC}}==
<langsyntaxhighlight lang="gwbasic"> 100 DIM V(255),C(255)
110 FOR I = 0 TO 3
120 FOR V = 1 TO 5
Line 212:
450 N = N + C( ASC ( MID$ (S$,I,1)))
460 NEXT I
470 RETURN</langsyntaxhighlight>
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">vRe: {/[aeiou]/}
cRe: {/[bcdfghjklmnpqrstvwxyz]/}
 
Line 224:
 
print ["Found" size vowels "vowels -" size unique vowels "unique"]
print ["Found" size consonants "consonants -" size unique consonants "unique"]</langsyntaxhighlight>
 
{{out}}
Line 232:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">str := "Now is the time for all good men to come to the aid of their country."
oV:= [], oC := [], v := c := o := 0
for i, ch in StrSplit(str)
Line 252:
 
MsgBox % result := str "`n`n" v+c+o " characters, " v " vowels, " c " consonants and " o " other"
. "`n" Vowels "`n" Consonants</langsyntaxhighlight>
{{out}}
<pre>Now is the time for all good men to come to the aid of their country.
Line 260:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f COUNT_HOW_MANY_VOWELS_AND_CONSONANTS_OCCUR_IN_A_STRING.AWK
BEGIN {
Line 280:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 288:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let ucase(c) =
Line 319:
 
let start() be
example("If not now, then when? If not us, then who?")</langsyntaxhighlight>
{{out}}
<pre>'If not now, then when? If not us, then who?': 10 vowels, 20 consonants.</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">/*
<lang C>/*
 
https://rosettacode.org/wiki/Count_how_many_vowels_and_consonants_occur_in_a_string
Line 400:
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 415:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">ucase = proc (c: char) returns (char)
if c>='a' & c<='z' then return(char$i2c(char$c2i(c)-32))
else return(c)
Line 455:
start_up = proc ()
example("If not now, then when? If not us, then who?")
end start_up</langsyntaxhighlight>
{{out}}
<pre>"If not now, then when? If not us, then who?": 10 vowels, 20 consonants.</pre>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. VOWELS-AND-CONSONANTS.
Line 513:
COUNT-CONSONANT.
INSPECT IN-STR TALLYING N-CONSONANTS FOR ALL CONSONANTS(C).
SET C UP BY 1.</langsyntaxhighlight>
{{out}}
<pre>If not now, then when? If not us, then who?
Line 520:
=={{header|Common Lisp}}==
 
<langsyntaxhighlight lang="lisp">(defun vowel-p (c &optional (vowels "aeiou"))
(and (characterp c) (characterp (find c vowels :test #'char-equal))))
 
Line 527:
 
(defun count-consonants (s)
(and (stringp s) (- (count-if #'alpha-char-p s) (count-vowels s))))</langsyntaxhighlight>
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub vowels_consonants(s: [uint8]): (vowels: intptr, consonants: intptr) is
Line 565:
end sub;
 
example("If not now, then when? If not us, then who?");</langsyntaxhighlight>
{{out}}
<pre>'If not now, then when? If not us, then who?': 10 vowels, 20 consonants.</pre>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Count how many vowels and consonants occur in a string. Nigel Galloway: August 1th., 202
type cType = Vowel |Consonant |Other
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)
printfn "%A" n
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 584:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: ascii combinators io kernel math.statistics prettyprint
sequences ;
 
Line 596:
"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@</langsyntaxhighlight>
{{out}}
<pre>
Line 608:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Dim As String cadena = """Forever the FreeBASIC programming language"""
Dim As Integer vocal = 0, consonante = 0
Line 642:
Print "In string occur"; vocal; " vowels"
Print "In string occur"; consonante; " consonants"
Sleep</langsyntaxhighlight>
{{out}}
<pre>
Line 653:
=={{header|Go}}==
Same approach as the Wren entry.
<langsyntaxhighlight lang="go">package main
 
import (
Line 687:
fmt.Printf("contains (distinct %d vowels and %d consonants.\n\n", len(vmap), len(cmap))
}
}</langsyntaxhighlight>
 
{{out}}
Line 705:
One of (at least) four possible meanings here:
 
<langsyntaxhighlight lang="haskell">import Control.Monad (join)
import Data.Bifunctor (bimap, first, second)
import Data.Bool (bool)
Line 756:
if p
then t
else f</langsyntaxhighlight>
{{Out}}
<pre>Unique vowels and consonants used, with counts:
Line 766:
Another of (at least) four possible meanings:
 
<langsyntaxhighlight lang="haskell">import Control.Monad (join)
import Data.Bifunctor (bimap)
import Data.Char (isAlpha)
Line 835:
 
both :: (a -> b) -> (a, a) -> (b, b)
both = join bimap</langsyntaxhighlight>
{{Out}}
<pre>33 'vowels and consonants'
Line 863:
Implementation (two tallies: vowels first, consonants second):
 
<langsyntaxhighlight lang="j">vowel=: (,toupper) 'aeiou'
consonant=: (,toupper) (a.{~97+i.16) -. vowel
vctally=: e.&vowel ,&(+/) e.&consonant</langsyntaxhighlight>
 
Examples:
 
<langsyntaxhighlight Jlang="j"> vctally 'Now is the time for all good men to come to the aid of their country.'
22 18
vctally 'Forever Action! programming language'
13 13</langsyntaxhighlight>
 
=={{header|JavaScript}}==
Line 884:
 
===Count of "Vowels and Consonants" ?===
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 910:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>33 "vowels and consonants"</pre>
 
===Counts of distinct vowels and distinct consonants seen ?===
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 999:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Distinct vowels: (aeiou, 5)
Line 1,006:
 
===Counts of vowel and consonant occurrences ?===
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 1,092:
 
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Vowel occurrences: 12
Line 1,099:
 
===Counts of occurrence for each vowel and consonant ?===
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 1,236:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Vowel counts:
Line 1,265:
 
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_letter: "a" <= . and . <= "z";
Line 1,290:
| pp, "";
 
task</langsyntaxhighlight>
{{out}}
<pre>
Line 1,314:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">isvowel(c) = c in ['a', 'e', 'i', 'o', 'u', 'A', 'E', "I", 'O', 'U']
isletter(c) = 'a' <= c <= 'z' || 'A' <= c <= 'Z'
isconsonant(c) = !isvowel(c) && isletter(c)
Line 1,335:
 
testvccount()
</langsyntaxhighlight>{{out}}
<pre>
String: Forever Julia programming language
Line 1,347:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 1,405:
printf "Consonants: %3d (Unique: %2d)\n" "${lettercnt[0]}" "${uniquecnt[0]}"
printf " Vowlels: %3d (Unique: %2d)\n" "${lettercnt[1]}" "${uniquecnt[1]}"
</langsyntaxhighlight>{{out}}
<pre>
Now is the time for all good men to come to the aid of their country.
Line 1,413:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="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"};
vowels = Join[vowels, ToUpperCase@vowels];
Line 1,420:
<|"vowels" -> StringCount[str, Alternatives @@ vowels],
"consonants" -> StringCount[str, Alternatives @@ conso],
"other" -> StringCount[str, Except[Alternatives @@ Join[vowels, conso]]]|></langsyntaxhighlight>
{{out}}
<pre><|"vowels" -> 22, "consonants" -> 24, "other" -> 11|></pre>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE VowelsAndConsonants;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;
FROM Strings IMPORT Length;
Line 1,478:
BEGIN
Display("If not now, then when? If not us, then who?");
END VowelsAndConsonants.</langsyntaxhighlight>
{{out}}
<pre>"If not now, then when? If not us, then who?": 10 vowels, 20 consonants.</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils
 
const
Line 1,508:
value(consonantCount, "consonant"))
 
vcCount("Now is the time for all good men to come to the aid of their country.")</langsyntaxhighlight>
 
{{out}}
Line 1,517:
=={{header|Pascal}}==
Standard “Unextended” Pascal (ISO standard 7185) does not really know the notion of strings:
<langsyntaxhighlight lang="pascal">program countHowManyVowelsAndConsonantsOccurInAString(input, output);
 
var
Line 1,546:
writeLn(vowelCount, ' vowels');
writeLn(consonantCount, ' consonants')
end.</langsyntaxhighlight>
{{in}}
The quick brown fox jumps over the lazy dog.
Line 1,554:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Count_how_many_vowels_and_consonants_occur_in_a_string
Line 1,569:
TEST ONE
Now is the time for all good men to come to the aid of their country.
Forever Perl Programming Language</langsyntaxhighlight>
{{out}}
<pre>
Line 1,584:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
Line 1,596:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,606:
===List comprehension===
Also using maps for counting individual characters.
<langsyntaxhighlight Picatlang="picat">main =>
S = "Count how many vowels and consonants occur in a string",
vowels(Vowels),
Line 1,630:
foreach(C in S, (Cs != "" -> membchk(C,Cs) ; true))
Map.put(C,Map.get(C,0)+1)
end.</langsyntaxhighlight>
 
{{out}}
Line 1,640:
 
===Recursion===
<langsyntaxhighlight Picatlang="picat">main =>
S = "Count how many vowels and consonants occur in a string",
vowels(Vowels),
Line 1,662:
Vs1 = Vs0
),
count_set(Set,Cs,Vs1,Vs).</langsyntaxhighlight>
 
{{out}}
Line 1,670:
=={{header|Python}}==
{{trans|Julia}}
<langsyntaxhighlight lang="python">def isvowel(c):
""" true if c is an English vowel (ignore y) """
return c in ['a', 'e', 'i', 'o', 'u', 'A', 'E', "I", 'O', 'U']
Line 1,698:
 
testvccount()
</langsyntaxhighlight>{{out}}<pre>
String: Forever Python programming language
Vowels: 11 (distinct 5)
Line 1,710:
 
Or, selecting another of the various possible meanings of an ambiguous task description:
<langsyntaxhighlight lang="python">'''Total and individual counts of vowel and consonant instances'''
 
from functools import reduce
Line 1,806:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>33 "vowels and consonants"
Line 1,830:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ bit
[ 0 $ "AEIOUaeiuo"
witheach [ bit | ] ] constant
Line 1,851:
echo say " vowels" cr
echo say " consonants"</langsyntaxhighlight>
 
{{out}}
Line 1,861:
'''OR''', depending on how you interpret the task…
 
<langsyntaxhighlight Quackerylang="quackery"> [ 0 $ "AEIOU"
witheach [ bit | ] ] constant is vowels ( --> n )
 
Line 1,883:
echo say " distinct vowels" cr
echo say " distinct consonants"</langsyntaxhighlight>
 
{{out}}
Line 1,894:
Note that the task '''does not''' ask for the '''total count''' of vowels and consonants, but for '''how many''' occur.
 
<syntaxhighlight lang="raku" perl6line>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>;
 
Line 1,902:
}
 
say letter-check "Forever Ring Programming Language";</langsyntaxhighlight>
 
{{out}}
Line 1,909:
=={{header|REXX}}==
=== version 1 ===
<langsyntaxhighlight lang="rexx">/* REXX */
Parse Arg s
If s='' Then
Line 1,931:
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'
Return</langsyntaxhighlight>
{{out}}
<pre>Forever Wren programming language
Line 1,938:
 
=== version 2 ===
<langsyntaxhighlight lang="rexx">/*REXX program counts the vowels and consonants (unique and total) in a given string. */
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.'
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
init: @.1='AEIOU'; @.2="BCDFGHJKLMNPQRSTVWXYZ"; upper $; $=space($,0); L=length($); return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,959:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">? "working..."
str = '"' + "Forever Ring Programming Language" + '"'
vowel = 0 vowels = [] for x in "AEIOUaeiou" add(vowels, x) next
Line 1,972:
? "In string occur " + vowel + " vowels"
? "In string occur " + (ltrc - vowel) + " consonants"
put "done..."</langsyntaxhighlight>
{{out}}
<pre>working...
Line 1,981:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">RE_V = /[aeiou]/
RE_C = /[bcdfghjklmnpqrstvwxyz]/
str = "Now is the time for all good men to come to the aid of their country."
Line 1,994:
 
grouped.each{|k,v| puts "#{k}: #{v.size}, #{v.uniq.size} unique."}
</syntaxhighlight>
</lang>
{{out}}
<pre>Consonants: 31, 13 unique.
Line 2,003:
=={{header|SNOBOL4}}==
{{works with|SNOBOL4, SPITBOL for Linux}}
<syntaxhighlight lang="snobol4">
<lang SNOBOL4>
* Program: countvc.sbl,
* To run: sbl countvc.sbl
Line 2,093:
 
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,122:
{{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.
<langsyntaxhighlight lang="ecmascript">import "/str" for Str
 
var vowels = "aeiou"
Line 2,150:
System.print("contains (total) %(vc) vowels and %(cc) consonants.")
System.print("contains (distinct) %(vmap.count) vowels and %(cmap.count) consonants.\n")
}</langsyntaxhighlight>
 
{{out}}
Line 2,165:
=={{header|X86 Assembly}}==
Translation of XPL0. Assemble with tasm, tlink /t
<langsyntaxhighlight lang="asm"> .model tiny
.code
.486
Line 2,295:
msg4 db " consonants."
crlf db 0Dh, 0Ah, 0
end start</langsyntaxhighlight>
 
{{out}}
Line 2,309:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">string 0; \use zero-terminated strings
int VTC, VDC, \vowel total count, vowel distinct count
CTC, CDC, \consonant total count, consonant distinct count
Line 2,344:
CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
10,327

edits