How to count vowels and consonants in Python
def classify_letter(string: str):
vowels = 0
consonants = 0
for i in string:
if i.casefold() in 'aeiou':
vowels += 1
elif i.casefold() in 'qwrtypsdfghjklzxcvbnm':
consonants += 1
return vowels, consonants
or with a list comprehension
def classify_letter(strng: str) -> tuple:
x = [i.casefold() in "aeiou" for i in strng if i.casefold() in string.ascii_lowercase]
return x.count(True), x.count(False)
^ I acknowledge this might not be the best way to implement that, im open to suggestions to make it shorter and optimized!
when passed "hello-"
these both return:
(2,3)
Counting vowels in an array
You're thinking about this too hard. Relax and let Python do the work:
nameList = [ "Euclid", "Archimedes", "Newton","Descartes", "Fermat", "Turing", "Euler", "Einstein", "Boole", "Fibonacci", "Nash"]
nameStr = ''.join(nameList).lower()
nVowels = len([c for c in nameStr if c in 'aeiou'])
print(f"The number of vowels in the list is {nVowels}.")
>>> The number of vowels in the list is 31.
Plenty of ways to skin a cat in Python :)
Count vowels in each of the strings using two dimensional array
For starters pay attention to that the function gets
is unsafe and is not supported by the C Standard. Instead use the standard function fgets
as for example
fgets( name[i], sizeof( name[i] ), stdin );
As for your problem then you need one more loop to traverse the array with vowels
for a given character in strings of the array name
.
For example
for (int i = 0; i < 3; i++)
{
for (int j = 0; name[i][j]; j++)
{
int k = 0;
while ( vowels[k] && name[i][j] != vowels[k] ) ++k;
if ( vowels[k] )
{
total++;
}
}
}
Another approach is to use your already written function like
for (int i = 0; i < 3; i++)
{
total += countVoweles( name[i] );
}
Instead of using a loop to traverse the array vowels
you could use standard C function strchr
declared in the header <string.h>
as for example
for (int i = 0; i < 3; i++)
{
for (int j = 0; name[i][j]; j++)
{
total += strchr( vowels, name[i][j] ) != NULL;
}
}
Count all vowels in 2D string
The loop logic is incorrect: vowels
should be initialized to 0
outside the outer loop and the return vowels;
statement moved outside the loop body.
Here is a modified version:
#include <stdio.h>
int vowels_count_2D(const int rows, const int cols, const char string[][cols]) {
int vowels = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (string[i][j] == 'a' || string[i][j] == 'e' ||
string[i][j] == 'i' || string[i][j] == 'o' ||
string[i][j] == 'u' || string[i][j] == 'A' ||
string[i][j] == 'E' || string[i][j] == 'I' ||
string[i][j] == 'O' || string[i][j] == 'U') {
vowels++;
}
}
}
return vowels;
}
int main() {
char strings[3][50] = { "hello WORLD", "hELLO", "Hello" };
printf("%d\n", vowels_count_2D(3, 50, strings));
return 0;
}
Instead of scanning the full 2D array, you should probably stop at the null terminator or each string:
#include <string.h>
int vowels_count_2D(const int rows, const int cols, char string[][cols]) {
int vowels = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols && string[i][j] != '\0'; j++) {
if (strchr("aeiouAEIOU", string[i][j])) {
vowels++;
}
}
}
return vowels;
}
How to count number of vowel - SQL Server
Replace all vowels with blank (to delete them) then subtract the length of the vowel-less string from the original length:
select
len(Column_String)
- len(
replace(replace(replace(replace(replace(
lower(Column_String), 'a', ''), 'e', ''), 'i', ''), 'o', ''), 'u', '')
) as vowel_count
from ...
As a function:
create function vowel_count(str nvarchar(1024))
returns int
as begin
return (
len(str) -
len(replace(replace(replace(replace(replace(
lower(str), 'a', ''), 'e', ''), 'i', ''), 'o', ''), 'u', ''));
end;
Related Topics
How to Run Ruby on Rails with Ruby 1.9X
How to Input Integer Value to an Array, Based Preceeding Row + Column Values
How to Set Up a Sinatra App Under Apache with Passenger
Ruby Working on Array Elements in Groups of Four
Ruby's <=> Operator and Sort Method
Rails 4 Error R14 on Heroku (Memory Quota Exceeded)
Convert String to Specific Datetime Format
How to Install Ruby on Rails 3.0 on Ubuntu 10.10
Invalid Multibyte Char (Utf-8) Error, Ruby
Replacing Text in One CSV Column Using Fastercsv
Best Practice to Mark Deprecated Code in Ruby
When to Use Struct Instead of Hash in Ruby
Does Ruby Have a String.Startswith("Abc") Built in Method
Ruby/Rails - Models Named with Two Words (Naming Convention Issues)
Programming Technique: How to Create a Simple Card Game
Calling Method in Parent Class from Subclass Methods in Ruby