13 Replies - 375 Views - Last Post: 12 May 2013 - 11:06 AM Rate Topic: -----

#1 evinda  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 12-May 13

Remove spaces of a string!

Posted 12 May 2013 - 09:10 AM

void remove(char *str) {
           char *s=str,*w=s;
           while (*s!= '\0') {
                  if (*s != ' '){
	              *w = *s;
	               w++;
                  } 
                  s++;
           }
           *w='\0';
            puts(str);
      }


That is a function that removes the spaces of a string...Could you explain me why the strings str gets the value of the string w???

Is This A Good Question/Topic? 0
  • +

Replies To: Remove spaces of a string!

#2 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: Remove spaces of a string!

Posted 12 May 2013 - 09:36 AM

Neither one gets the value of the other. They simply point to the same string.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: Remove spaces of a string!

Posted 12 May 2013 - 09:43 AM

Let's say the string in question is "a b". Then somewhere in memory, you have the following sequence of chars:

'a' ' ' 'b' '\0'


str is a pointer that points to the beginning of that sequence, so we could visualize it like this:

 /---str
'a' ' ' 'b' '\0'



At the beginning of your function, s and w are set to be equal to str, so we get this:

 /---str
 /---w
 /---s
'a' ' ' 'b' '\0'



Now the first time through the while loop, *w = *s is executed, which does nothing because *w and *s are the same value ('a'). Then w and s are incremented. So the situation now looks like this:

 /---str
 |   /---w
 |   /---s
'a' ' ' 'b' '\0'



Now s is incremented, but w isn't, so we get this:


 /---str
 |   /---w
 |   |   /---s
'a' ' ' 'b' '\0'



In the next step *w = *s is executed again and this time that has a visible effect, putting a 'b' at the position that w is pointing to (because s currently points to a 'b'). So we now have:

 /---str
 |   /---w
 |   |   /---s
'a' 'b' 'b' '\0'



Again both w and s are incremented, giving us:

 /---str
 |       /---w
 |       |   /---s
'a' 'b' 'b' '\0'



Now the while loop ends because s points to a 0-byte and after the while loop *w is set to '\0', giving us the final result:

 /---str
 |       /---w
 |       |    /---s
'a' 'b' '\0' '\0'



So str now points to the beginning of the string "ab".

This post has been edited by sepp2k: 12 May 2013 - 09:49 AM

Was This Post Helpful? 4
  • +
  • -

#4 evinda  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 12-May 13

Re: Remove spaces of a string!

Posted 12 May 2013 - 09:58 AM

But then,why,when I add the command:printf("%c",*str); in the loop,I get the values at which the pointer *src show when I run the program ???
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: Remove spaces of a string!

Posted 12 May 2013 - 10:02 AM

When you do printf("%c", *str);, you're printing the character to which str points, i.e. the first character of the string. In my example that'd be the character 'a'. I'm not really sure what else you'd expect to be printed.
Was This Post Helpful? 1
  • +
  • -

#6 evinda  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 12-May 13

Re: Remove spaces of a string!

Posted 12 May 2013 - 10:09 AM

Yes,but I also incremented str...Running the programm with the string "a b",i get the following: *str=a
*str=
*str=b...
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: Remove spaces of a string!

Posted 12 May 2013 - 10:14 AM

In the code you've posted, you do not increment str. If you change the code, so that you do increment str every time you increment s, then the puts(str) at the end of the function will no longer print the modified string - it will print nothing (because str will then point to a 0-byte after the loop - like s in my last "picture").
Was This Post Helpful? 1
  • +
  • -

#8 evinda  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 12-May 13

Re: Remove spaces of a string!

Posted 12 May 2013 - 10:32 AM

From which command is it visible that str points to the beginning of the string "ab"?
Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: Remove spaces of a string!

Posted 12 May 2013 - 10:34 AM

From the fact that puts(str) will print "ab" (assuming the code you've posted at the beginning of this thread - if you increment str at any point, it will no longer point to the beginning of "ab").
Was This Post Helpful? 1
  • +
  • -

#10 evinda  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 12-May 13

Re: Remove spaces of a string!

Posted 12 May 2013 - 10:39 AM

A ok...But I haven't understood yet why str points to the beginning of the sequence at the beggining of the programm... :/
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: Remove spaces of a string!

Posted 12 May 2013 - 10:45 AM

Because if you pass an array to a function, it turns into a pointer to its first argument. So if you have a char array that contains the string "a b" (i.e. an array that contains the chars 'a', ' ', 'b' and '\0') and then call your function with that array as its argument, str will be a pointer to the beginning of that array.

This post has been edited by sepp2k: 12 May 2013 - 10:48 AM

Was This Post Helpful? 1
  • +
  • -

#12 evinda  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 12-May 13

Re: Remove spaces of a string!

Posted 12 May 2013 - 10:54 AM

And...because of the fact that *s=str and *w=s,when w gets the value "ab",*s takes the same value and *str too???Or am I wrong??? :/
Was This Post Helpful? 0
  • +
  • -

#13 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: Remove spaces of a string!

Posted 12 May 2013 - 10:59 AM

str doesn't take any new value. The value of the str is the address at which the string "a b" starts. Let's say that address is 42. So str is 42 and, since s=str and w=s, s and w also have the value 42. During the loop s and w get incremented a lot, so at the end of the loop, s has the value 45 (the address of the original 0-byte) and w has the value 44 (the address of the new 0-byte). The value of str is still 42. The only thing that's changed are the values that are stored at the addresses 43 and 44. Specifically the value at address 43 used to be ' ', but is now 'b', and the value at address 44 used to be 'b', but is now '\0'.
Was This Post Helpful? 1
  • +
  • -

#14 evinda  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 12-May 13

Re: Remove spaces of a string!

Posted 12 May 2013 - 11:06 AM

Ok...I understood it now!!!! :bigsmile: Thank you very much!!!!!!!!!!!! :rolleyes:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1