Your task is to design a string calculator with two functions: Create a class with 2 overloaded member functions. The function has to return the concatenated string if it receives 4 characters as an argument. The function has to return the list of words if it receives a single string argument. Sample Input b o o K C++ programming lab Sample Output booK C++ programming lab
Question
Your task is to design a string calculator with two functions:
Create a class with 2 overloaded member functions.
The function has to return the concatenated string if it receives 4 characters as an argument.
The function has to return the list of words if it receives a single string argument.
Sample Input
b
o
o
K
C++ programming lab
Sample Output
booK
C++
programming
lab
Solution
Here is a simple implementation in Python:
class StringCalculator:
def calculate(self, *args):
if len(args) == 4:
return ''.join(args)
elif len(args) == 1:
return args[0].split()
else:
return "Invalid input"
# Testing the class
calc = StringCalculator()
print(calc.calculate('b', 'o', 'o', 'K')) # Output: booK
print(calc.calculate('C++ programming lab')) # Output: ['C++', 'programming', 'lab']
In this Python class, we have a single method calculate that is overloaded to handle different types of input. If the method receives 4 arguments, it concatenates them into a single string. If it receives a single string argument, it splits the string into a list of words. If the input does not match any of these conditions, it returns "Invalid input".
Similar Questions
What is the function to concatenate two strings in C programming?
Write a function, lenWords(STRING), that takes a string as an argumentand returns a tuple containing length of each word of a string.For example, if the string is "Come let us have some fun", thetuple will have (4, 3, 2, 4, 4, 3)
Iterate through the words list and concatenate each word to result
Problem:define a recursive function to generate all Anagrams of a given stringTest Case 1:Enter a word: abcabcacbbacbcacabcbaTest Case 2:Enter a word: ababba
Write a C program using string functions to perform the following functionalities: Assume: The array size as 50.To concatenate two strings.To find the length of a string.To copy one string to another.To find the size of a string.To compare two strings.Write each functionality as different function and call it from the main program.Sample InputEnter String1: VelloreEnter String2: ChennaiSample Output
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.