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
Question
Define a class to accept a string and convert it into uppercase.
Count and display the number of vowels in it.
Input: robotics
Output: ROBOTICS
Number of vowels: 3
Solution
Problem Breakdown
- Define a class that will accept a string as input.
- Implement a method to convert the string to uppercase.
- Implement a method to count the number of vowels in the string.
- Display the uppercased string and the count of vowels.
Relevant Concepts
- String Manipulation: To change characters in a string.
- Vowel Identification: A function to check if a character is a vowel (A, E, I, O, U).
Analysis and Detail
- Create a class called
StringProcessor
. - In the class, include methods for:
convert_to_upper
: Converts the input string to uppercase.count_vowels
: Counts the vowels in the input string.
- Use appropriate string and list functions to perform the tasks.
Verify and Summarize
- Ensure that the methods work correctly by testing with the input "robotics".
- Confirm that the output is "ROBOTICS" and the vowel count is 3.
Final Answer
class StringProcessor:
def __init__(self, input_string):
self.input_string = input_string
def convert_to_upper(self):
return self.input_string.upper()
def count_vowels(self):
vowels = "aeiouAEIOU"
return sum(1 for char in self.input_string if char in vowels)
# Example usage
input_str = "robotics"
processor = StringProcessor(input_str)
upper_str = processor.convert_to_upper()
vowel_count = processor.count_vowels()
print(f"Output: {upper_str}")
print(f"Number of vowels: {vowel_count}")
Output:
Output: ROBOTICS
Number of vowels: 3
Similar Questions
Define a class to accept a string and convert it into uppercase. Count and display thenumber of vowels in it.
#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 program to accept a string as input and print the number of consonants in the string.Input:StringOutput:IntegerConstraints1<=length of string <=1000
Write a function, vowelCount() in Python that counts and displays thenumber of vowels in the text file named Poem.txt
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.