split string into chunks

Discussion in 'C Programming' started by mdefoor, Dec 8, 2005.

  1. mdefoor

    mdefoor Guest

    I've written the following sample to split a string into chunks. Is
    this the right approach or is there perhaps a better way to accomplish
    getting chunks of a string?

    #include <stdio.h>
    #include <string.h>

    #define BUFSIZE 10

    int main(int argc, char *argv[]) {
    int i;
    int nbr;
    char buff[256];
    char tmpChr[1];
    char *buffer;

    sprintf(buff, "%s", "this is a longer buffer");
    buffer = strdup(buff);

    char tmpBuff[strlen(buffer) + 1];
    *tmpBuff = '\0';

    i = 0;
    nbr = 0;

    printf("buffer = %s len = %d\n", buffer, strlen(buffer));

    do {
    if (i < BUFSIZE) {
    sprintf(tmpChr, "%c", buffer[nbr]);
    strcat(tmpBuff, tmpChr);
    i++;
    nbr++;
    if (nbr == strlen(buffer)) {
    printf("--%s-- i = %d nbr = %d\n", tmpBuff, i, nbr);
    }
    }
    else {
    printf("--%s-- i = %d nbr = %d\n", tmpBuff, i, nbr);
    i = 0;
    *tmpBuff = '\0';
    }
    } while (buffer[nbr] != '\0');
    }
     
    mdefoor, Dec 8, 2005
    #1
    1. Advertising

  2. mdefoor

    Joe Estock Guest

    mdefoor wrote:
    > I've written the following sample to split a string into chunks. Is
    > this the right approach or is there perhaps a better way to accomplish
    > getting chunks of a string?


    There is a better way. sprintf() and strcat() are expensive, especially
    when you can explicitly do it yourself.

    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > #define BUFSIZE 10
    >
    > int main(int argc, char *argv[]) {
    > int i;
    > int nbr;
    > char buff[256];
    > char tmpChr[1];

    /* what in the world is this? Get rid of the above and use char *tmpChr;
    at the very least */
    > char *buffer;
    >
    > sprintf(buff, "%s", "this is a longer buffer");
    > buffer = strdup(buff);
    >
    > char tmpBuff[strlen(buffer) + 1];

    /* tsk, tsk. declarations belong at the top of the function. */
    > *tmpBuff = '\0';
    >
    > i = 0;
    > nbr = 0;
    >
    > printf("buffer = %s len = %d\n", buffer, strlen(buffer));
    >
    > do {
    > if (i < BUFSIZE) {
    > sprintf(tmpChr, "%c", buffer[nbr]);
    > strcat(tmpBuff, tmpChr);
    > i++;
    > nbr++;
    > if (nbr == strlen(buffer)) {
    > printf("--%s-- i = %d nbr = %d\n", tmpBuff, i, nbr);
    > }
    > }
    > else {
    > printf("--%s-- i = %d nbr = %d\n", tmpBuff, i, nbr);
    > i = 0;
    > *tmpBuff = '\0';
    > }
    > } while (buffer[nbr] != '\0');
    > }
    >


    You seem to be fairly new at programming in c so pay close attention to
    my comments as they will help you (unless I've made a typo somewhere - I
    compiled this and it worked correctly, but it's late so anything could
    happen).

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h> /* exit() and friends */

    #define BUFSIZE 10

    int chunk_split(const char *src, char **dest, int len);

    /**
    * Split a string into several lines with each line being
    * no more than len characters.
    *
    * NOTE: There are actually several different ways of doing
    * this. For instance, we could make a copy of src and use
    * *tmp++ = *srccpy++ instead of *tmp++ = src. Both would
    * do exactly the same thing and most modern compilers will
    * optimise it either way. Personal preference I suppose.
    *
    * @param src Source buffer
    * @param dest Destination buffer
    * @param len Maximum length of a line
    * @return 0 on success, nonzero indicates error
    */
    int chunk_split(const char *src, char **dest, const int len)
    {
    char *tmp = *dest;
    int i = 0;
    int j = 0;
    int srclen = strlen(src);

    for(i = 0; i < srclen; i++)
    {
    for(j = 0; j < len; j++)
    *tmp++ = src[i++];

    *tmp++ = '\n';
    i--;
    }

    *tmp++ = '\0';

    return(0);
    }

    int main(int argc, char **argv)
    {
    int length = 0;
    char *buffer = NULL;

    /* you can get your source buffer from anywhere. this is
    * easier for testing, imho. */
    if(argc != 2)
    {
    printf("Usage: %s \"some string\"\n", argv[0]);
    exit(EXIT_FAILURE);
    }

    /* plenty of room for our added newlines */
    length = (strlen(argv[1]) + (strlen(argv[1]) / 10));

    /* always test your calls to malloc otherwise gremlins
    * might come and take you away to SIGSEGVland. */
    if((buffer = malloc(length + 1)) == NULL)
    {
    perror("malloc");
    exit(EXIT_FAILURE);
    }

    chunk_split(argv[1], &buffer, BUFSIZE);

    /* tada */
    printf("before: %s\n----------\n%s\n----------\n", argv[1], buffer);

    free(buffer);

    return(0);
    }

    You gave it an honest attempt, and for that I took the time to rewrite
    this into something a little more usable. Hope it helps.

    Joe
     
    Joe Estock, Dec 8, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Katie
    Replies:
    5
    Views:
    257
    Katie
    Sep 15, 2006
  2. Sam Kong
    Replies:
    5
    Views:
    246
    Rick DeNatale
    Aug 12, 2006
  3. Drew Olson
    Replies:
    33
    Views:
    970
    Drew Olson
    Dec 2, 2006
  4. Stanley Xu
    Replies:
    2
    Views:
    618
    Stanley Xu
    Mar 23, 2011
  5. Norah Jones
    Replies:
    0
    Views:
    120
    Norah Jones
    Mar 27, 2013
Loading...

Share This Page