27.4k views
1 vote
Create a function generateString(char, val) that returns a string with val number of char characters concatenated together. For example generateString('a', 7) will return aaaaaaa.

2 Answers

4 votes

I have write a very simple code for you in python and i hope it will help you a lot.


def generateString(char, val):

print(char * val)

Step-by-step explanation:

This is how you can create your function in python and this function will give you the desired output.


How to Call a function:

generateString('a',12)

this is how you can call the function to get output.

I hope you get the idea.



User KrisDrOid
by
5.4k points
4 votes

Answer:

The solution code is written in Python.

  1. def generateString(char, val):
  2. output = ""
  3. for i in range(val):
  4. output += char
  5. return output
  6. print(generateString('a', 7))

Step-by-step explanation:

Firstly, let's create a function generateString() that take two input arguments, char and val.

To generate a string with val number of char, we need a string variable, output, to hold the string value. Let's initialize the output with an empty string (Line 2)

Next, we can create a for loop that will repeat the loop for val number of time (using the range() method) and keep adding the same char to the output string (Line 5).

At last return the output string (Line 7).

User Lenn Dolling
by
5.7k points