Using strlen and a cutter to cut playername to two main letters - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Using strlen and a cutter to cut playername to two main letters (
/showthread.php?tid=429474)
Using strlen and a cutter to cut playername to two main letters -
BullseyeHawk - 09.04.2013
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.
Re: Using strlen and a cutter to cut playername to two main letters -
Universal - 09.04.2013
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.
Re: Using strlen and a cutter to cut playername to two main letters -
Jefff - 09.04.2013
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);
Re: Using strlen and a cutter to cut playername to two main letters -
BullseyeHawk - 10.04.2013
Thank you Universal and Jefff,
I'd use Jefff's stock, it looks a bit better then the untested.