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

Add ed example
No edit summary
(Add ed example)
 
(5 intermediate revisions by 5 users not shown)
Line 615:
 
 
procedure DoVowelConsonatTestDoVowelConsonantTest(Memo: TMemo);
{Test two strings for vowels/consonants}
begin
Line 641:
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
proc count s$ . .
for c$ in strchars s$
c = strcode c$
if c >= 97 and c <= 122
c -= 32
.
if c >= 65 and c <= 91
c$ = strchar c
if c$ = "A" or c$ = "E" or c$ = "I" or c$ = "O" or c$ = "U"
vow += 1
else
cons += 1
.
.
.
print "There are " & vow & " vowels and " & cons & " consonants"
.
count "Now is the time for all good men to come to the aid of their country."
</syntaxhighlight>
 
=={{header|ed}}==
 
Uses non-portable [[GNU Ed]] <tt>i</tt> suffix for case-insensitive matching.
 
<syntaxhighlight lang="sed">
H
,p
,j
s/[^[:alpha:]]//g
# Turn vowels into exclamations and consonants into commas
s/[aeiou]/!/gi
s/[b-df-hj-np-tv-z]/,/gi
# Sort the values to put vovels first
# (repeat as many times as necessary)
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
s/(,+)(!+)/\2\1/g
# Split vowels and consontants lines
s/!,/!\
,/
,p
Q
</syntaxhighlight>
 
{{out}}
 
<pre>$ cat vowels-consonants.ed | ed -lEGs vowels-consonants.input
Now is the time for all good men to come to the aid of their country
!!!!!!!!!!!!!!!!!!!!!!
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,</pre>
 
And then count the exclamation marks and commas manually?
 
=={{header|F_Sharp|F#}}==
Line 2,147 ⟶ 2,218:
In string occur 19 consonants
done...</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.8}}
≪ "AEIOU" → str voy
≪ (0,0) 1 str SIZE '''FOR''' j
str j DUP SUB
'''IF''' DUP "a" ≥ OVER "z" ≤ AND '''THEN''' NUM 32 - CHR '''END'''
'''IF''' DUP "A" ≥ OVER "Z" ≤ AND '''THEN''' voy SWAP POS 1 (0,1) IFTE + '''ELSE''' DROP '''END'''
'''NEXT'''
≫ ≫ ''''VOYCON'''' STO
 
"Now is the time for all good men to come to the aid of their country." '''VOYCON'''
{{out}}
<pre>
1: (22,31)
</pre>
 
=={{header|Ruby}}==
Line 2,167 ⟶ 2,254:
Vowels: 22, 5 unique.
Other: 16, 2 unique.
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">use std::io ;
 
//string supposed to contain ascii letters only!
fn main() {
println!("Enter a string!");
let mut inline : String = String::new( ) ;
io::stdin( ).read_line( &mut inline ).unwrap( ) ;
let entered_line : &str = &*inline ;
let vowels = vec!['a' , 'e' , 'i' , 'o' , 'u' , 'A' , 'E' , 'I' ,
'O' , 'U'] ;
let numvowels = entered_line.trim( ).chars( ).filter( | c |
vowels.contains( c ) ).count( ) ;
let consonants = entered_line.trim( ).chars( ).filter( | c |
! vowels.contains( c ) && c.is_ascii_alphabetic( )).count( ) ;
println!("String {:?} contains {} vowels and {} consonants!" ,
entered_line.trim( ) , numvowels , consonants ) ;
}</syntaxhighlight>
{{out}}
<pre>
Enter a string!
Rust shares properties of procedural and functional languages!
String "Rust shares properties of procedural and functional languages!" contains 21 vowels and 33 consonants!
</pre>
 
Line 2,290 ⟶ 2,402:
{{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.
<syntaxhighlight lang="ecmascriptwren">import "./str" for Str
 
var vowels = "aeiou"
110

edits