Using strlen and a cutter to cut playername to two main letters
#1

Lets say we have the name 'Firstname_Lastname'.
I'm making a system, that needs the first capital letters of it, that means 'Firstname_Lastname' should turn to 'FL'.
F - from (F)irstname
L - from (L)astname

I couldn't have successed to get thoes two letters.
Could anyone help me out with this ? Thanks.
Reply
#2

pawn Код:
new name[MAX_PLAYER_NAME], letters[2][2];
//Get the first letter as we already know whats the position
format(letters[0], 2, "%s", name[0]);
//Loop through all of the letters in the name
for (new i = 0; i < MAX_PLAYER_NAME; i++) {
    //Check if this is the underscore we are looking for first
    if (name[i] == '_') {
        //Its the underscore, meaning the next letter will be +1 index ahead
            format(letters[1], 2, "%s", name[i+1]);
        // We dont need to search anything anymore as we found our letters already
            break;
    }
}

printf("%s %s", letters[0], letters[1]);
Not sure if works, havent tested this.
Reply
#3

pawn Код:
ExtractChars(const source[],dest[])
{
    new len = strlen(source);
    if(!(2 < len < MAX_PLAYER_NAME)) return;

    new
        i,
        a,
        First = (-1),
        Second = (-1);

    if('A' <= source[i++] <= 'Z')
        First = source[i-1];

    while((len = source[i++]))
        if(len == '_')
        {
            if(Second == -1)
                Second = source[i];
            a++;
        }

    if(a < 1 || a > 1) return;

    dest[0] = First;
    dest[1] = Second;
    return;
}
Usage

pawn Код:
new Chars[2];
ExtractChars("Firstname_Lastname",Chars);
printf("%c.%c - %s",Chars[0],Chars[1],Chars);
Reply
#4

Thank you Universal and Jefff,
I'd use Jefff's stock, it looks a bit better then the untested.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)