Knowee
Questions
Features
Study Tools

Define a class to accept a string and convert it into uppercase. Count and display thenumber of vowels in it.

Question

Define a class to accept a string and convert it into uppercase. Count and display the number of vowels in it.

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

Solution

To define a class that accepts a string, converts it into uppercase, counts the number of vowels, and displays the result, we can follow these steps:

Step 1: Class Definition

We will create a class named StringProcessor with methods to handle the tasks.

Step 2: Initializing the Class

The class will have an initializer that accepts a string and assigns it to an instance variable.

Step 3: Method for Conversion

A method will be defined to convert the string to uppercase.

Step 4: Method for Counting Vowels

Another method will count the vowels in the string.

Step 5: Display the Results

Finally, a method will display the uppercase string and the count of vowels.

Implementation

Here's the code that implements the above steps:

class StringProcessor:
    def __init__(self, input_string):
        self.input_string = input_string
        
    def convert_to_uppercase(self):
        self.uppercase_string = self.input_string.upper()
        
    def count_vowels(self):
        vowels = 'AEIOU'
        self.vowel_count = sum(1 for char in self.uppercase_string if char in vowels)

    def display_results(self):
        print("Uppercase String:", self.uppercase_string)
        print("Number of Vowels:", self.vowel_count)

# Example of usage:
input_string = "Hello World"
processor = StringProcessor(input_string)
processor.convert_to_uppercase()
processor.count_vowels()
processor.display_results()

Explanation

  1. Class Initialization: The __init__ method initializes the class with the input string.
  2. Uppercase Conversion: convert_to_uppercase converts the string to uppercase using the .upper() method.
  3. Vowel Counting: count_vowels iterates through the uppercase string and counts the vowels using a generator expression.
  4. Displaying Results: display_results prints out the uppercase string and the number of vowels.

This code accomplishes the task effectively by utilizing class functionalities in Python.

This problem has been solved

Similar Questions

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

#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.

Write a short Python function num_vowels(text) that counts the number of vowels ina given character string

Write a function, vowelCount() in Python that counts and displays thenumber of vowels in the text file named Poem.txt

Write a program to accept a string as input and print the number of consonants in the string.Input:StringOutput:IntegerConstraints1<=length of string <=1000

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.