Knowee
Questions
Features
Study Tools

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

🧐 Not the exact question you are looking for?Go ask a question

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:

  1. We define a function count_vowels_and_words that takes an input string.
  2. We define vowels as a string containing all the vowels in both lower and upper case.
  3. We initialize vowel_count and word_count to 0.
  4. We iterate over each character in the input string. If the character is in vowels, we increment vowel_count.
  5. We split the input string into words using the split method, 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 to word_count.
  6. The function returns vowel_count and word_count.
  7. We call this function with an example string "Hello, how are you?" and print the number of vowels and words.

This problem has been solved

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.

1/3

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.