char *colorize_string(const char *name, const char *colors)
{
    static char color_buf[2*MSL];
    char *p_name = name; //may need to make this const char *p_name
    int i = 0;
    int j = 0;

    if (colors[0] == '\0' || name[0] == '\0' ||
        (strlen (name) + strlen (colors) > MSL))
        return;

    color_buf[0] = '\0';

    while (p_name)
    {
        color_buf[i++] = '&';
        color_buf[i++] = colors[j++];
        j %= strlen(colors);
        color_buf[i++] = *p_name++;
    }

    color_buf[i] = '\0';
    return color_buf;
}

One word of warning:
*pname++
is not ANSI defined.  If you want to be safe, do:
        color_buf[i++] = *p_name;
        p_name++;

Also, not sure if it's
        j %= strlen(colors);
or
        j %= strlen(colors) - 1;


To call:

colorize_string(name, "Rrw");


----- Original Message ----- 
From: "Robin Björklund" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Wednesday, May 05, 2004 11:00 PM
Subject: colorize_string


Hi, sorry to bother you guys.
I'm still new to coding and you'll notice if you take a look on the code
I'm posting below.
I use this piece of code to colorize a name 3 - 12 letters.

For example if my name is Desindit and I would like to colorize it.
I would do something like colorize_string(name, "R", "r", "w");
And the string would become "&RDe&rs&win&rd&Rit"
If you notice this code isn't finished yet, since it only colorize
strings with 8 letters. Though looking at my code
It's pretty much a mess in my eyes, I figured I should use some loops to
colorize the strings, but since the colorizing will be different
depending on how long the name is, I figured it would still be a mess
using loops.
I would be greatful to any help and comments to the mess. Ohh and I
better get some sleep now and when I wake up I'll realize how stupid I
was that I didn't get some sleep before writing code. ;)

char *colorize_string (const char *str, const char *c1, const char *c2,
const char *c3 )
{
char newbuf[MSL];
static char tempbuf[MSL];

tempbuf[0] = '\0';

if (strlen(str) == 7)
{
sprintf(newbuf, "&%c%c", *c1, str[0]);
strcat(tempbuf, newbuf);
sprintf(newbuf, "%c", str[1]);
strcat(tempbuf, newbuf);
sprintf(newbuf, "&%c%c", *c2, str[2]);
strcat(tempbuf, newbuf);
sprintf(newbuf, "&%c%c", *c3, str[3]);

strcat(tempbuf, newbuf);
sprintf(newbuf, "&%c%c", *c2, str[4]);
strcat(tempbuf, newbuf);
sprintf(newbuf, "&%c%c", *c1, str[5]);

strcat(tempbuf, newbuf);
sprintf(newbuf, "%c", str[6]);
strcat(tempbuf, newbuf);
}

if (strlen(str) == 8)
{
sprintf(newbuf, "&%c%c", *c1, str[0]);
strcat(tempbuf, newbuf);
sprintf(newbuf, "%c", str[1]);
strcat(tempbuf, newbuf);
sprintf(newbuf, "&%c%c", *c2, str[2]);
strcat(tempbuf, newbuf);
sprintf(newbuf, "&%c%c", *c3, str[3]);

strcat(tempbuf, newbuf);
sprintf(newbuf, "%c", str[4]);
strcat(tempbuf, newbuf);
sprintf(newbuf, "&%c%c", *c2, str[5]);
strcat(tempbuf, newbuf);
sprintf(newbuf, "&%c%c", *c1, str[6]);

strcat(tempbuf, newbuf);
sprintf(newbuf, "%c", str[7]);
strcat(tempbuf, newbuf);
}

return tempbuf;
}


-- 
ROM mailing list
[email protected]
http://www.rom.org/cgi-bin/mailman/listinfo/rom


Reply via email to