Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > split string into chunks

Reply
Thread Tools

split string into chunks

 
 
mdefoor
Guest
Posts: n/a
 
      12-08-2005
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');
}

 
Reply With Quote
 
 
 
 
Joe Estock
Guest
Posts: n/a
 
      12-08-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Splitting a list into even size chunks in python? Norah Jones Python 0 03-27-2013 08:06 AM
How to use String.split to split a mixed encoding string(partencoded in gbk, part encoded in utf-8) Stanley Xu Ruby 2 03-23-2011 02:06 PM
Splitting a CSV file into 40,000 line chunks Drew Olson Ruby 33 12-02-2006 01:06 AM
How to break up binaryWrite file into multiple chunks Katie ASP General 5 09-15-2006 02:05 PM
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57