By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

split string into chunks

P: n/a
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');
}

Dec 8 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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[i]. 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
Dec 8 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.