find the number of alphabet vowels and words in input using string
Question
find the number of alphabet vowels and words in input using string
Solution
Sure, here is a Python solution for your problem:
def count_vowels_and_words(input_string):
vowels = "aeiouAEIOU"
vowel_count = 0
word_count = 0
for char in input_string:
if char in vowels:
vowel_count += 1
words = input_string.split()
word_count = len(words)
return vowel_count, word_count
input_string = "Hello, how are you?"
vowel_count, word_count = count_vowels_and_words(input_string)
print("Number of vowels: ", vowel_count)
print("Number of words: ", word_count)
In this code:
- We define a function
count_vowels_and_wordsthat takes an input string. - We define
vowelsas a string containing all the vowels in both lower and upper case. - We initialize
vowel_countandword_countto 0. - We iterate over each character in the input string. If the character is in
vowels, we incrementvowel_count. - We split the input string into words using the
splitmethod, which splits a string into a list where each word is a list item. We then get the length of this list to find the number of words, and assign this toword_count. - The function returns
vowel_countandword_count. - We call this function with an example string "Hello, how are you?" and print the number of vowels and words.
Similar Questions
#Write a Python function count_vowels(text) that takes a string as input and returns the count of vowels (a, e, i, o, u) in the input string, ignoring case.
Define a class to accept a string and convert it into uppercase. Count and display thenumber of vowels in it.Input: roboticsOutput: ROBOTICSNumber of vowels: 3
Define a class to accept a string and convert it into uppercase. Count and display thenumber of vowels in it.
Read any string and display the vowels
Write a program in C to count the number of vowels and consonants in a string. You can use fgets to read the characters.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.