Jump to content

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

no edit summary
(Added Arturo implementation)
No edit summary
Line 1,842:
Other: 16, 2 unique.
</pre>
 
=={{header|SNOBOL4}}==
{{works with|SNOBOL4, SPITBOL for Linux}}
<lang SNOBOL4>
* Program: countvc.sbl,
* To run: sbl countvc.sbl
* Description: Count how many vowels and consonants occur in a string
* Comment: Tested using the Spitbol for Linux version of SNOBOL4
 
 
* Function SQUEEZE will remove some characters from s (string).
* Parameter c is the character set to keep or remove.
* If parameter kr is 1, then only characters in c will be kept.
* If it is 0, then the characters in c will be removed.
* Parameter kr defaults to 1. So if it is null or not 0 or 1,
* then it becomes 1.
define('squeeze(s,c,kr)pre')
:(squeeze_end)
squeeze
kr = (eq(size(kr),0) 1,kr)
kr = (eq(kr,1) kr, eq(kr,0) kr, 1)
eq(kr,1) :s(kr1)
kr0
* Exclude character set
s ? breakx(c) . pre span(c) = :f(kr2)
squeeze = squeeze pre
:(kr0)
kr1
* Include character set
s ? breakx(c) span(c) . pre = :f(kr2)
squeeze = squeeze pre
:(kr1)
kr2
:(return)
squeeze_end
 
 
* Function POPT will populate table t with counts
* for each, unique character from string.
* It first standarizes string to only contain
* upper and lower case letters and then replaces
* upper case letters with lower case letters.
* It returns t converted to an array.
define('popt(string,t)s,c') :(popt_end)
popt
s = squeeze(string,&lcase &ucase)
s = replace(s,&ucase,&lcase)
popt1
s ? len(1) . c = ?(t[c] = t[c] + 1) :s(popt1)
popt = convert(t,'ARRAY') :s(return)f(freturn)
popt_end
 
 
* Function OUTPUTARRAY will output array as well as return the number
* of unique array elements and the sum of their counts,
* separated by the |.
define('outputarray(a)i,sum,n') :(outputarray_end)
outputarray
i = i + 1
output = a[i,1] ', ' a[i,2] :f(outputarray2)
sum = sum + a[i,2]
n = i
:(outputarray)
outputarray2
outputarray = n "|" sum
:(return)
outputarray_end
 
 
alphabet = &lcase &ucase
vowels = 'aeiouAEIOU'
consonants = squeeze(alphabet,vowels,0) ;* Remove vowels
 
v = table()
c = table()
 
s = "Now is the time for all good men to come to the aid of their country."
 
output = s
 
vs = squeeze(s,vowels,1) ;* Remove all characters if not a vowel
va = popt(vs,v) ;* Put unique characters into array with counts
ret = outputarray(va) ;* Output character array
ret ? breakx("|") . n len(1) rem . sum
output = "Number of unique vowels is " n ', total=' sum
 
cs = squeeze(s,consonants,1) ;* Remove all characters if not a consonant
vc = popt(cs,c) ;* Put unique characters into array with counts
ret = outputarray(vc) ;* Output character array
ret ? breakx("|") . n len(1) rem . sum
output = "Number of unique consonants is " n ', total=' sum
 
END
</lang>
{{out}}
<pre>
Now is the time for all good men to come to the aid of their country.
o, 9
i, 4
e, 6
a, 2
u, 1
Number of unique vowels is 5, total=22
n, 3
w, 1
s, 1
t, 7
h, 3
m, 3
f, 2
r, 3
l, 2
g, 1
d, 2
c, 2
y, 1
Number of unique consonants is 13, total=31
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-str}}
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.