Count how many vowels and consonants occur in a string: Difference between revisions
Count how many vowels and consonants occur in a string (view source)
Revision as of 14:11, 3 April 2024
, 2 months agono edit summary
No edit summary |
No edit summary |
||
(11 intermediate revisions by 8 users not shown) | |||
Line 568:
{{out}}
<pre>'If not now, then when? If not us, then who?': 10 vowels, 20 consonants.</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|StdCtrls,SysUtils}}
Makes extensive use of "sets" to find vowels and consonants and determine if they are unique.
<syntaxhighlight lang="Delphi">
const TestStr1: string = 'Delphi is delightful.';
const TestStr2: string = 'Now is the time for all good men to come to the aid of their country.';
type TCharSet = set of 'a'..'z';
procedure VowelConsonant(S: string; Memo: TMemo);
{Find number of total and unique vowels and consonants}
const Vows: TCharSet = ['a','e','i','o','u'];
const Cons: TCharSet = ['a'..'z']-['a','e','i','o','u'];
var VowSet,ConSet: TCharSet;
var VCnt,CCnt,UVCnt,UCCnt,I: integer;
procedure HandleMatch(C: char; var Cnt,UCnt: integer; var CSet: TCharSet);
{Handle set matching and incrementing operations}
begin
Inc(Cnt);
if not (C in CSet) then Inc(UCnt);
Include(CSet,C);
end;
begin
Memo.Lines.Add(S);
VCnt:=0; CCnt:=0;
UVCnt:=0;UCCnt:=0;
S:=LowerCase(S);
for I:=1 to Length(S) do
begin
{Test if character is vowel or consonant}
if S[I] in Vows then HandleMatch(S[I],VCnt,UVCnt,VowSet)
else if S[I] in Cons then HandleMatch(S[I],CCnt,UCCnt,ConSet);
end;
Memo.Lines.Add('Vowels: '+IntToStr(VCnt));
Memo.Lines.Add('Consonants: '+IntToStr(CCnt));
Memo.Lines.Add('Unique Vowels: '+IntToStr(UVCnt));
Memo.Lines.Add('Unique Consonants: '+IntToStr(UCCnt));
end;
procedure DoVowelConsonantTest(Memo: TMemo);
{Test two strings for vowels/consonants}
begin
VowelConsonant(TestStr1,Memo);
Memo.Lines.Add('');
VowelConsonant(TestStr2,Memo);
end;
</syntaxhighlight>
{{out}}
<pre>
Delphi is delightful.
Vowels: 6
Consonants: 12
Unique Vowels: 3
Unique Consonants: 8
Now is the time for all good men to come to the aid of their country.
Vowels: 22
Consonants: 31
Unique Vowels: 5
Unique Consonants: 13
</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|F_Sharp|F#}}==
Line 653 ⟶ 747:
<syntaxhighlight lang="futurebasic">include "NSLog.incl"
void local fn
CFCharacterSetRef vowelSet = fn CharacterSetWithCharactersInString( @"aeiou" )
CFMutableCharacterSetRef
fn MutableCharacterSetRemoveCharactersInString(
*vowels = len( fn StringComponentsSeparatedByCharactersInSet( string, vowelSet ) ) - 1
*consonents = len( fn StringComponentsSeparatedByCharactersInSet( string,
end fn
void local fn DoIt
long index, vowels,
CFArrayRef strings = @[@"abcdefghijklmnop345qrstuvwxyz",
@"The quick brown fox jumps over the lazy dog",
@"The answer my friend is
for index = 0 to len(strings) - 1
fn
NSLog(@"\"%@\" contains %ld vowels and %ld
next
end fn
Line 679 ⟶ 773:
{{out}}
<pre>
"abcdefghijklmnop345qrstuvwxyz" contains 5 vowels and 21
"The quick brown fox jumps over the lazy dog" contains 11 vowels and 24
"The answer my friend is
</pre>
=={{header|Go}}==
Line 908 ⟶ 1,000:
vctally 'Forever Action! programming language'
13 13</syntaxhighlight>
An alternative expression for <code>consonant</code> could be:
<syntaxhighlight lang="j">consonant=: (a.#~2|'@Z`z'I.a.) -. vowel</syntaxhighlight>
=={{header|JavaScript}}==
Line 1,702 ⟶ 1,798:
<pre>[vowels = 15,consontants = 30,rest = 9]</pre>
=={{header|Plain English}}==
<syntaxhighlight lang="plainenglish">To run:
Start up.
Put "Now is the time for all good men to come to the aid of their country." into a string.
Find a vowel count and a consonant count of the string.
Write the double-quote byte then the string then the double-quote byte on the console.
Write "Number of vowels: " then the vowel count on the console.
Write "Number of consonants: " then the consonant count on the console.
Wait for the escape key.
Shut down.
To find a vowel count and a consonant count of a string:
Slap a substring on the string.
Loop.
If the substring is blank, exit.
Put the substring's first's target into a letter.
If the letter is any vowel, bump the vowel count.
If the letter is any consonant, bump the consonant count.
Add 1 to the substring's first.
Repeat.</syntaxhighlight>
{{out}}
<pre>
"Now is the time for all good men to come to the aid of their country."
Number of vowels: 22
Number of consonants: 31
</pre>
=={{header|Python}}==
Line 1,941 ⟶ 2,064:
{{out}}
<pre>5 vowels and 8 consonants occur in the string "Forever Ring Programming Language"</pre>
=={{header|REBOL}}==
<syntaxhighlight lang="rebol">
REBOL [
Title: "Count how many vowels and consonants occur in a string"
Date: 21-Dec-2022
Author: "Earldridge Jazzed Pineda"
]
countVowelsConsonants: func [string] [
vowels: [#"a" #"e" #"i" #"o" #"u"]
consonants: [#"b" #"c" #"d" #"f" #"g" #"h" #"j" #"k" #"l" #"m" #"n" #"p" #"q" #"r" #"s" #"t" #"v" #"w" #"x" #"y" #"z"]
vowelCount: 0
consonantCount: 0
foreach character string [
if (find consonants character) <> none [consonantCount: consonantCount + 1]
if (find vowels character) <> none [vowelCount: vowelCount + 1]
]
return reduce [vowelCount consonantCount]
]
string: "Count how many vowels and consonants occur in a string"
counts: countVowelsConsonants string
print [mold string "has" pick counts 1 "vowels and" pick counts 2 "consonants"]
</syntaxhighlight>
{{out}}
<pre>{Count how many vowels and consonants occur in a string} has 15 vowels and 30 consonants</pre>
=={{header|REXX}}==
Line 2,014 ⟶ 2,168:
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,034 ⟶ 2,204:
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,157 ⟶ 2,352:
{{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="
var vowels = "aeiou"
|