Number of each vowel in a string
Aim: Implement
a Program to count the number of each vowel in a string
Algorithm:
1.Start
2.Enter
the string s.
3.Take
each character in the string and compare with ‘a,e,i,o,u’.
4.If
found increment corresponding counter by one.
5.Print
the counter values.
6.Stop
Program:
# string of vowels
vowels = 'aeiou'
# take input from the
user
ip_str =
input("Enter a string: ")
# make it suitable for
caseless comparisions
ip_str =
ip_str.casefold()
# make a dictionary
with each vowel a key and value 0
count =
{}.fromkeys(vowels,0)
# count the vowels
for char in ip_str:
if char in count:
count[char] += 1
print(count)
Output:
Enter a string: Hello,
have you tried our turorial section yet?
{'e': 5, 'u': 3,
'o': 5, 'a': 2, 'i': 3}4)
0 Comments:
Post a Comment