-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?

Xin Cheng
  • 1,432
  • 10
  • 17
WorldXp
  • 11
  • 3
  • 3
    Try a reversed approach. If char is *not* a space, concatenate it to a temp string for return. – S3DEV Oct 14 '22 at 18:53
  • You are not actually modifying the string. And that would be generally impossible since strings are ***immutable***. – PM 77-1 Oct 14 '22 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. – TimH Oct 14 '22 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](//meta.stackoverflow.com/q/334822/4518341). 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 [ask] in general. – wjandrea Oct 14 '22 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. – wjandrea Oct 14 '22 at 19:22
  • `"".join([ c for c in s if c!=' '])` – Xin Cheng Oct 14 '22 at 19:49
  • @Dibbi Barua, within your function `remove_space(s)`, just `return "".join([ c for c in s if c!=' '])` – Xin Cheng Oct 14 '22 at 20:21

3 Answers3

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'
Barry the Platipus
  • 9,594
  • 2
  • 6
  • 30
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"))
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.

Kamar
  • 342
  • 1
  • 6
  • 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. – wjandrea Oct 14 '22 at 19:14
  • 1
    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 Oct 14 '22 at 19:21
  • @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 Oct 14 '22 at 19:24
  • @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. – Kamar Oct 14 '22 at 19:28
  • @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.) – wjandrea Oct 14 '22 at 19:34