1

I'm trying to create a function which converts the following string:

1110100010000000101000011000011110000000000000000000000000111101110000110101100111

to:

1110100010000000 1010 0001 1000 0111 1000 0000 0000 0000 0000 0000 0011 1101 1100 0011 0101 1001 11

(groups of four 1's/0's)

Some function like the following:

convert(char *src_buffer,char *dst_buffer,int offset){

}

where "offset" is 16 in the above case.

Here's the code I've tried sofar:

char *tmp=(char*)malloc(1000*sizeof(char));
strncpy(tmp,buffer,i);
tmp[i+1]=' ';
for(int j=0;j<sizeof(buffer);j++){
    strcpy(tmp+sizeof(tmp),buffer+(4*j));
    tmp[(5*j)+1]=' ';
}

But it just won't work...

Please help! I'm hoping there's a C guru out there who can help me.

Here's some updated code I've been working on:

char *tmp=(char*)malloc(1000*sizeof(char));
strncpy(tmp,buffer,offset);
tmp[offset+1]=' ';
int k=offset+1;
for(int j=i;j<strlen(buffer);j+=4){
    tmp[k]=buffer[j];
    tmp[k+1]=buffer[j+1];
    tmp[k+2]=buffer[j+2];
    tmp[k+3]=buffer[j+3];
    tmp[k+5]=' ';
    k+=5;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Eamorr
  • 9,872
  • 34
  • 125
  • 209
  • 7
    Where, EXACTLY, do you want the spaces? I see groups of 4 and 3, not to mention the big string at the start. – QED Dec 04 '12 at 14:31
  • 1
    Offset is 16. The spaces should separate groups of four. The fact that the last chunk is of length three is as a result of the offset specification. – Eamorr Dec 04 '12 at 14:32
  • 4
    Please, [don't cast the return value of `malloc()`, in C](http://stackoverflow.com/a/605858/28169). Also, don't scale by `sizeof (char)`, it's just an annoying way of writing `1`. Also, what is `i`? – unwind Dec 04 '12 at 14:32
  • 2
    What are the `i` and `offset`? There seems no correlation to the function definition you gave and the function declaration you gave. – askmish Dec 04 '12 at 14:32
  • 1
    The big string at the start is the result of the offset, it appears OP wants a space immediately after the offset, then every fifth space (4 chars then space). The string in the example I'm sure contains typos. – Joe Dec 04 '12 at 14:32
  • 1
    The groups of 3 at the end are curious but the first block is explained by him mentioning the offset of 16. – Bernd Elkemann Dec 04 '12 at 14:32
  • If `buffer` is a pointer, `sizeof(buffer)` returns the _size of the pointer_, not what it points to. – Some programmer dude Dec 04 '12 at 14:33
  • @joe: Its really ambiguous until OP guides us in a proper way! – askmish Dec 04 '12 at 14:33
  • @eznme The block of 3 is a result of the offset being defined 16. If offset=17, there would be a block of 2 at the end. – Eamorr Dec 04 '12 at 14:34
  • 5
    There are several blocks of three at the end though - why ? – Paul R Dec 04 '12 at 14:34
  • 1
    The algorithm you are using is bit unclear. Try to explain what you are trying to achieve. – codewarrior Dec 04 '12 at 14:35
  • @PaulR Yes, you're right. Fixed the OP now. I was clicking my keyboard too fast. Sorry for the ambiguity. – Eamorr Dec 04 '12 at 14:36
  • Joachim got it. I'm outta here – QED Dec 04 '12 at 14:38

2 Answers2

3

Copy the first offset characters to the temporary buffer. Then loop over each character in the remaining original buffer, one by one, copying them into the temporary buffer. Every fourth loop add a space to the temporary buffer.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

Try something like this:

int len = strlen(src);
char *dst= malloc(len * sizeof *dst * 2);
/* copy the first (offset) bytes */
strncpy(dst, src, offset); 

for(i=j=offset; j<len; i++, j++){
    /* add a whitespace and once every 5 characters */
    if ((i-offset)%5 == 0) { 
       dst[i++] = ' ';
    }
    dst[i] = src[j];
}
/* null-terminate string */
dst[i]=0;

As a side note, If every character is followed by a space (which is not the case) then you would need maximum twice as much as the original string, so no need to allocate 1000 bytes.

iabdalkader
  • 17,009
  • 4
  • 47
  • 74
  • Side-side note. No need to `*sizeof(char)` since that will always be `1` – Mike Dec 04 '12 at 14:58
  • @Mike actually no that's implementation defined and not guaranteed by the standard. – iabdalkader Dec 04 '12 at 15:02
  • 1
    This: `6.5.3.4 paragraph 4: "When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1."` Is incorrect? – Mike Dec 04 '12 at 15:07
  • @mux as Mike pointed out it is quite the contrary, that is the only guarantee made by the standard for `sizeof`. – Joe Dec 04 '12 at 15:08
  • @Mike you're right, I was confused for a minute, I removed it. – iabdalkader Dec 04 '12 at 15:27
  • @Eamorr I updated the code because it didn't handle the first whitespace correctly with offsets not divisible by 4. – iabdalkader Dec 04 '12 at 15:42