-1

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?

7
  • 3
    Try a reversed approach. If char is not a space, concatenate it to a temp string for return. Commented Oct 14, 2022 at 18:53
  • You are not actually modifying the string. And that would be generally impossible since strings are immutable. Commented Oct 14, 2022 at 18:54
  • To add to what @PM77-1 said, strings are immutable, meaning that you can't directly "change" a string. In your code, the variable char in your for loop is just a copy of the character in string. So, setting char = temp will only change the variable char to have the value "", not remove the space. Commented Oct 14, 2022 at 18:59
  • Welcome to Stack Overflow! Please take the tour. Your solution sort of works, except that print() adds a newline on every loop. I've closed your question as a duplicate accordingly. However, printing is not the same as returning, so you'll need to find a new approach. Now, please read the homework questions FAQ. The way this question is written, you're asking us to do your homework for you, which is not what we're about. Instead, to make this a constructive question for SO, you'd want to focus on your own approach. See also How to Ask in general. Commented Oct 14, 2022 at 19:09
  • 1
    To clarify, I mean, you could rewrite the question as, for example, "How can I remove spaces from a string without using any string methods?" and that'd be valid for SO. You can edit if you want. Commented Oct 14, 2022 at 19:22

3 Answers 3

0
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'
Sign up to request clarification or add additional context in comments.

3 Comments

I thought there is nothing to explain, apart from the very descriptive variable names. How would you explain this better that the actual function body?You'd just write the (pseudo) code again. @S3DEV
@XinCheng see OP's post: replace() and join() cannot be used. There is nothing to edit here.
... well ... ok, if you insist.
0

Try this: Create a variable temp, concat the char if it is != " " and then return temp

def remove_space(string):
    temp = ""
    for char in string:
        if char != " ":
            temp += char
    return temp
print(remove_space("welcome to computer science"))

Comments

0

Simple answer: When you write char = temp, you are basically assigning the variable to a new value instead of overriding t in the original string.

What you are doing is equivalent to:

a = 3
b = a
b = 2

Even though you tell b to be equal a, you reassign it later to be 2. If you print(a) you will see that its value will still be the same.

CS-Dojo has a great video on this https://youtu.be/Z1Yd7upQsXY?t=1002 you can start at minute 16 where he explains variable assignment.

5 Comments

How does this answer the question? OP's code never returns string anyway, so it doesn't seem like they're expecting it to be modified.
It addresses a mistake the OP did where they (in my interpretation) believed that they could delete a space by assigning char = temp where temp is an empty string.
@mkrieger1 Is that really a mistake? The spaces in fact never get printed because of that assignment. It seems like this is a red herring.
@wjandrea from what I understand, OP does not want to return the string, he's just printing the characters of the string one by one. I pointed out why his code wont replace char with an empty space.
@Mohamad OP's assignment does say to return the string. But if it said to print the string, OP's code does actually replace the spaces, however it still prints a newline. (I actually closed the question as a duplicate on these two points.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.