Thread: Flip words in a char array.

  1. #16
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    char *y=calloc(1,strlen(s)+1);
    char *p=s+strlen(s);
    You dint check the return value of calloc and dont really see the point of this
    Code:
    if( !strchr(delims,*s) )
        strcat(y,s);
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  2. #17
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    What's the difference between calloc and malloc?

  3. #18
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by mherald81 View Post
    What's the difference between calloc and malloc?
    calloc - allocated the memory and initialises the allocated memory to 0
    malloc - allocated memory but dosn't initialise the allocated memory

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #19
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    Quote Originally Posted by ssharish2005 View Post
    calloc - allocated the memory and initialises the allocated memory to 0
    malloc - allocated memory but dosn't initialise the allocated memory

    ssharish
    Thanks. I can see why that would be useful for this program.

  5. #20
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    Can you explain this loop a bit for me?

    Code:
      while( p!=s )
        if( strchr(delims,*--p) )
          strcat(y,p+1),strncat(y,p,1),*p=0;
    It's mainly the strcat line that I'm lost on because all of that is on one line with a comma separating them. Thanks.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mherald81
    It's mainly the strcat line that I'm lost on because all of that is on one line with a comma separating them.
    That is just a (misguided) attempt to make the code more concise. It could have been written as:
    Code:
    char *wordrev(char *s, const char *delims)
    {
        char *y = calloc(1, strlen(s) + 1);
        char *p = s + strlen(s);
        while (p != s)
        {
            if (strchr(delims, *--p))
            {
                strcat(y, p + 1);
                strncat(y, p, 1);
                *p = 0;
            }
        }
        if (!strchr(delims, *s))
        {
            strcat(y, s);
        }
        strcpy(s, y);
        free(y);
        return s;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    Quote Originally Posted by laserlight View Post
    That is just a (misguided) attempt to make the code more concise. It could have been written as:
    Code:
    char *wordrev(char *s, const char *delims)
    {
        char *y = calloc(1, strlen(s) + 1);
        char *p = s + strlen(s);
        while (p != s)
        {
            if (strchr(delims, *--p))
            {
                strcat(y, p + 1);
                strncat(y, p, 1);
                *p = 0;
            }
        }
        if (!strchr(delims, *s))
        {
            strcat(y, s);
        }
        strcpy(s, y);
        free(y);
        return s;
    }
    So strcat(y, p+1) concatenates without the space at the beginning of p and strncat(y,p,1) concatenates the space at the end. And *p=0 sets where p is pointing to in memory to 0? And the loop will continue until p and s are pointing at the same address in memory?
    Last edited by mherald81; 10-17-2010 at 09:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending a simple email in C++?
    By Coukapecker in forum C++ Programming
    Replies: 6
    Last Post: 04-09-2010, 12:36 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. char array of words
    By sujeet1 in forum C++ Programming
    Replies: 5
    Last Post: 05-25-2007, 07:27 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM