Selecting part of a string
#1

I couldn't find an answer for my problem, so I decided to post it here.

I have a function which should color-select a part of a string.
For example, I have the next string: "This is a simple 'text line' in my string".
I need somehow to make the new string look like: "This is a simple {FFFFFF}text line{NORMAL COLOR} in my string".

NORMAL COLOR - the default color I work with
FFFFF - Another color to "select" the needed text.

I need a way to replace the first " ' " with the selected color, to check where it ends and to set it back to the normal color (replace the second " ' " with the normal hex color - {}).

It would be easily done with scanf, but I also need to check each time how many selected words I have in my string. Each word which is between " ' " tags, should be color-selected.


I would be glad to get some help, thank you.
Reply
#2

Bump (second page)
Reply
#3

Here is a very simple example of what you want, I hope you'll get the basic idea.

pawn Код:
new str[] = "This is a simple 'text line' in my string";
    new text[128]; //This is a simple {FFFFFF}text line{NORMAL COLOR} in my string
    new count = 0;
    new character = false;
    new i = 0;
    while (str[i] != EOS)
    {
        if (str[i] == ''')
        {
            if (character)
            {
                text[count] = '
{';
                count++;
                text[count] = '
0';
                count++;
                text[count] = '
0';
                count++;
                text[count] = '
0';
                count++;
                text[count] = '
0';
                count++;
                text[count] = '
0';
                count++;
                text[count] = '
0';
                count++;
                text[count] = '
}';
                count++;
            }
            else
            {
                text[count] = '
{';
                count++;
                text[count] = '
F';
                count++;
                text[count] = '
F';
                count++;
                text[count] = '
F';
                count++;
                text[count] = '
F';
                count++;
                text[count] = '
F';
                count++;
                text[count] = '
F';
                count++;
                text[count] = '
}';
                count++;
            }
            character = !character;
            i++;
        }
        text[count] = str[i];
        i++;
        count++;
    }
    text[count] = EOS;
    print(text);
Note that you will need to implenet stuff like error checking (to much or to little ' chars, etc ...)
Reply
#4

Quote:
Originally Posted by dice7
Посмотреть сообщение
Here is a very simple example of what you want, I hope you'll get the basic idea.

pawn Код:
new str[] = "This is a simple 'text line' in my string";
    new text[128]; //This is a simple {FFFFFF}text line{NORMAL COLOR} in my string
    new count = 0;
    new character = false;
    new i = 0;
    while (str[i] != EOS)
    {
        if (str[i] == ''')
        {
            if (character)
            {
                text[count] = '
{';
                count++;
                text[count] = '
0';
                count++;
                text[count] = '
0';
                count++;
                text[count] = '
0';
                count++;
                text[count] = '
0';
                count++;
                text[count] = '
0';
                count++;
                text[count] = '
0';
                count++;
                text[count] = '
}';
                count++;
            }
            else
            {
                text[count] = '
{';
                count++;
                text[count] = '
F';
                count++;
                text[count] = '
F';
                count++;
                text[count] = '
F';
                count++;
                text[count] = '
F';
                count++;
                text[count] = '
F';
                count++;
                text[count] = '
F';
                count++;
                text[count] = '
}';
                count++;
            }
            character = !character;
            i++;
        }
        text[count] = str[i];
        i++;
        count++;
    }
    text[count] = EOS;
    print(text);
Note that you will need to implenet stuff like error checking (to much or to little ' chars, etc ...)
Thank you dice7, it works perfectly for me
I'll try to understand what you were doing here.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)