SA-MP Forums Archive
strcount function? - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: strcount function? (/showthread.php?tid=154170)



strcount function? - lolumadd - 12.06.2010

Is there a command out there that can can:
-Count the amount of characters given as in a input in a string

Example:
pawn Код:
new string[128], count;
format(string, sizeof(string), "341,141,514,141");
count = strcount(string, ",");
print(count);
OUTPUT: 3

Can anyone help me make a function like that..?


Re: strcount function? - ziomal432 - 12.06.2010

pawn Код:
str_countcharacters(str[], char)
{
    new c;
    for(new i, l = strlen(str); i < l; i++)
        if(str[i] == char)
            c++;
    return c;
}
Not tested, but should work

Example:
pawn Код:
printf("%d", str_countcharacters("341,141,514,141", ','));



Re: strcount function? - lolumadd - 12.06.2010

Quote:
Originally Posted by ziomal432
pawn Код:
str_countcharacters(str[], char)
{
    new c;
    for(new i, l = strlen(str); i < l; i++)
        if(str[i] == char)
            c++;
    return c;
}
Not tested, but should work

Example:
pawn Код:
printf("%d", str_countcharacters("341,141,514,141", ','));
Thanks! It works!