Count how many vowels and consonants occur in a string: Difference between revisions
Content added Content deleted
(→{{header|Quackery}}: second interpretation of task) |
Catskill549 (talk | contribs) (added AWK) |
||
Line 93: | Line 93: | ||
5 vowels and 9 consonants (distinct) |
5 vowels and 9 consonants (distinct) |
||
5 vowels and 9 consonants (total) |
5 vowels and 9 consonants (total) |
||
</pre> |
|||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f COUNT_HOW_MANY_VOWELS_AND_CONSONANTS_OCCUR_IN_A_STRING.AWK |
|||
BEGIN { |
|||
str = "Now is the time for all good men to come to the aid of their country." |
|||
printf("%s\n",str) |
|||
str = toupper(str) |
|||
for (i=1; i<=length(str); i++) { |
|||
if (substr(str,i,1) ~ /[AEIOU]/) { |
|||
count_vowels++ |
|||
} |
|||
else if (substr(str,i,1) ~ /[BCDFGHJKLMNPQRSTVWXYZ]/) { |
|||
count_consonants++ |
|||
} |
|||
else { |
|||
count_other++ |
|||
} |
|||
} |
|||
printf("%d characters, %d vowels, %d consonants, %d other\n",length(str),count_vowels,count_consonants,count_other) |
|||
exit(0) |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
Now is the time for all good men to come to the aid of their country. |
|||
69 characters, 22 vowels, 31 consonants, 16 other |
|||
</pre> |
</pre> |
||