Flip words in a char array.

This is a discussion on Flip words in a char array. within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #16
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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,682
    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
    22,166
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 10:24 AM.

Page 2 of 2 FirstFirst 12
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, 01:36 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. char array of words
    By sujeet1 in forum C++ Programming
    Replies: 5
    Last Post: 05-25-2007, 08: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, 10:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21