3

I have a string AAbbCC what I need is to copy the first two and add them to an array then copy the middle two and add them to an array and finally the last two and add them to an array.

this is what I do:

char color1[2];
char color2[2];
char color3[2];

strncpy(color1, string, 2); // I take the first two characters and put them into color1

// now I truncate the string to remove those AA values:

string = strtok(string, &color1[1]);

// and when using the same code again the result in color2 is bbAA:

strncpy(color2, string, 2); 

it passes those bb but also AA from previous one .. even though the array has only two places, when I use strtol on that it gives me some big value and not 187 which I'm looking for .. how to get rid of that ? or how to make it work other way? Any advice would be appreciated.

Markus
  • 686
  • 1
  • 11
  • 18
  • char * strings are null terminated. If you want to use colorX in your code as strings (which I assume you do when converting 0xbb to 187), you need to extend them by one byte ad set this byte to 0. – Dmitry Alexeyev Apr 29 '12 at 20:37
  • If you just want to skip 2 chars, why don't you simply do something like `strncpy(color2, string+2, 2); color2[2] = '\0'` etc? – Anthales Apr 29 '12 at 20:39
  • yeah thank both of you .. I figured it out :) and @Anthales thanks for pointing out that I don't need strtok at all – Markus Apr 29 '12 at 21:11
  • You mention that you are looking for 187, but that is not evident from the rest of the question. Why don't you use `strtoul` on the larger string instead of cutting it into pieces first? – Mr Lister Apr 29 '12 at 21:12
  • @MrLister actually I was looking for a tool which will cut the color represented by hexadecimal values into 3 different values then I will convert them to decimal values (RGB) but I already got that with adding one space for '\0' into my array :) – Markus Apr 30 '12 at 08:14

2 Answers2

5

First, you need add +1 in size for the \0.

char color1[3];
char color2[5];

and then:

strncpy(color1, string, 2);
color1[3] = '\0';

strncpy(color2, string + 2, 4); 
color2[4] = '\0';

Assuming that

char *string = "AAbbCC"; 

printf("color1 => %s\ncolor2 => %s\n", color1, color2);

The output is:

color1 => AA
color2 => bbCC

I hope this help you.

UPDATE

You can write a substr() function to get part of string(from x to y) and then copy to your string.

char * substr(char * s, int x, int y)
{
    char * ret = malloc(strlen(s) + 1);
    char * p = ret;
    char * q = &s[x];

    assert(ret != NULL);

    while(x  < y)
    {
        *p++ = *q++;
        x ++; 
    }

    *p++ = '\0';

    return ret;
}

Then:

char *string = "AAbbCC"; 
char color1[3];
char color2[4];
char color3[5];
char *c1 = substr(string,0,2);
char *c2 = substr(string,2,4);
char *c3 = substr(string,4,6);

strcpy(color1, c1);
strcpy(color2, c2);
strcpy(color3, c3);

printf("color1 => %s, color2 => %s, color3 => %s\n", color1, color2, color3);

The output:

color1 => AA, color2 => bb, color3 => CC

And Don't forget:

free(c1);
free(c2);
free(c3);
Jack
  • 16,276
  • 55
  • 159
  • 284
  • I don't think he intended to copy `bbCC` into `color2`, he probably only wanted `bb` (as he implies in his first sentence). – Anthales Apr 29 '12 at 20:51
  • 1
    actually Anthales is right, but the solution provided by Jack contains enough information, I would've been able to tune it for my needs anyway. Unfortunately I can only give accept to one person and zmbq was the first one with actual correct answer. If I'll happen to have 15 reputation I would vote up your question. Thank you for your help. – Markus Apr 29 '12 at 21:34
1

Well, color1 and color2 are two bytes long - you have no room for the \0 terminator. When you look at one of them as a string, you get more characters that you wished for. If you look at them as two characters, you'll get the right result.

You should define them as 3 characters long and put the \0 at the end.

zmbq
  • 38,013
  • 14
  • 101
  • 171