#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    159
    Rep Power
    10

    Substring(start, end, str) in c


    Hi guys. As the title suggests, I want to write a function

    substring(int start, int stop, char* str) but I dont really know how to since i am only a newbie C programmer. Any suggestions would be really helpful

    P.S. I know how to get the substring of a string from a starting position till the end using pointer arithmetic but I have no idea how I can have a starting position and an end position. Thanks
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2004
    Posts
    1,676
    Rep Power
    133
    Something along this line?
    Code:
    #include <stdio.h>
    
    void substring(int start, int stop, const char *text)
    {
       printf("%.*s\n", stop - start, &text[start]);
    }
    
    int main()
    {
       static const char text[] = "The quick brown fox jumps over the lazy dog.";
       substring(4,13,text);
       return 0;
    }
    
    /* my output
    quick bro
    */
    Could you clarify by describing the expected output for a given input?
    Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    159
    Rep Power
    10
    I havent tried your code yet but this is the kind of output i am expecting. I really cant see how this works though. I am not saying it doesnt (i am sure it does). Just that I cant understand the printf line. Thanks very much
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    159
    Rep Power
    10
    Just tried it and it works but I want to be able to pass as a parameter a pointer to a char instead of a char array.

    What does %.*s does in printf? i know what %s does but I have to understand it since I want to return the substring at the end of the method instead of printing it out. Thx
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2004
    Posts
    1,676
    Rep Power
    133
    Regarding the format specifiers, just find a good reference for printf.

    Regarding "returning a substring", in C and C++ you can't return an array, so you can't return a C-style string (which is an array of characters terminated by a null character). The usual methods are to use dynamic memory and return a pointer to the start of the string, use a static string, or the preferred method: to pass a destination array along with its size and place the output there.

    Using this method, and choosing sprintf to write to the destination string, it might look like this.
    Code:
    #include <stdio.h>
    
    char *substring(size_t start, size_t stop, const char *src, char *dst, size_t size)
    {
       int count = stop - start;
       if ( count >= --size )
       {
          count = size;
       }
       sprintf(dst, "%.*s", count, src + start);
       return dst;
    }
    
    int main()
    {
       static const char text[] = "The quick brown fox jumps over the lazy dog.";
       char a[10], b[5];
       printf("substring = \"%s\"\n", substring(4, 13, text, a, sizeof a));
       printf("substring = \"%s\"\n", substring(4, 13, text, b, sizeof b));
       return 0;
    }
    
    /* my output
    substring = "quick bro"
    substring = "quic"
    */
    Last edited by Dave Sinkula; January 18th, 2006 at 08:12 PM. Reason: Braino: count is stop - start, not start - stop.
    Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.

IMN logo majestic logo threadwatch logo seochat tools logo