3 Replies - 253 Views - Last Post: 08 October 2012 - 10:49 PM Rate Topic: -----

#1 izic  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 197
  • Joined: 15-January 09

Understanding this piece of code.

Posted 08 October 2012 - 06:22 PM

Hi can someone help me understand what this function does exactly?
I know if(isspace(*src)) checks for whitespace in the array but I'm unsure what everything else does/means.
I'm guessing while(isspace(*(src+1))) deletes each whitespace too?

void modifyspace(char *dest, char *src)
{
   for( ; *src; dest++, src++)
   {
      *dest = *src;
      if(isspace(*src))

            while(isspace(*(src+1)))
               src++;
   }

   *dest = '\0';
}



Is This A Good Question/Topic? 0
  • +

Replies To: Understanding this piece of code.

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Understanding this piece of code.

Posted 08 October 2012 - 06:27 PM

http://www.cplusplus...orial/pointers/

read the part about pointer arithmetic
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,569
  • Joined: 25-December 09

Re: Understanding this piece of code.

Posted 08 October 2012 - 06:31 PM

It looks to be copying the characters from *src to *dest, it is replacing multiple white space characters with one space character.


Jim
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: Understanding this piece of code.

Posted 08 October 2012 - 10:49 PM

Have you considered running some tests?
char test[] = "this is   a       test";
char result[100];
printf("Before=%s\n", test);
modifyspace(result, test);
printf("After=%s\n", result);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1