Issue
I'm a freshman CS student. This is one of the exercises we had in class:
Create a function called
remove_space(string)
:
- The function takes in a string as input
- The function returns a new string, which is generated by removing all the spaces in the given string.
- Example:
remove_space(“welcome to computer science”)
returns“welcometocomputerscience”
I have looked around for solutions online but we aren't allowed to use any advanced functions like split(), replace(), join() or anything of that sort. I'm looking for the most primitive way to solve this without using those that would make sense coming from a freshman CS student.
This is what I have so far:
def remove_space(string):
temp = ""
for char in string:
if char == " ":
char = temp
print(char)
print("----------------#1---------------")
remove_space("Welcome to computer science")
What am I doing wrong?
Solution
def remove_space(string):
result_string = ''
for character in string:
if character != ' ':
result_string += character
return result_string
remove_space('welcome to computer science')
Result:
'welcometocomputerscience'
Answered By - Barry the Platipus
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.