Valid letters problem - 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: Valid letters problem (
/showthread.php?tid=541429)
Valid letters problem -
uZ1 - 11.10.2014
Код:
new ValidChars[][] =
{
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4",
"5", "6", "7", "8", "9", ".", ")", "(", "[", "]", "=", "$", "@", " "
};
Код:
stock IsValidCharacters(const characters[])
{
new checkamount;
new lengh = strlen(characters);
for(new char = 0; char < sizeof(ValidChars); char ++) {
if(strfind(characters, ValidChars[char], true) == 0) { checkamount++; }
if(checkamount == lengh) { return true; }
}
return false;
}
Whats wrong here?
I just want to check a text for these characters, I need this for file creations.
Re: Valid letters problem -
Chenko - 11.10.2014
It's probably easier to check for invalid characters. But looking at this the problem is probably that you aren't considering the end string character which is "\0" I believe. If the string only contains the valid characters you have listed then checkamount has to equal length - 1
Re: Valid letters problem -
Vince - 11.10.2014
This is probably the shortest I can write this. And you get a free handy function with it.
pawn Код:
stock IsValidCharacters(characters[])
{
for(new i, j = strlen(characters); i < j; i++) // go over each character in the string separately
{
if(!('A' <= characters[i] <= 'Z' || 'a' <= characters[i] <= 'z' || '0' <= characters[i] <= '9' || in_array(characters[i], { '.', ')', '(', '[', ']', '=', '$', '@' })))
return false;
}
return true;
}
stock in_array(needle, const haystack[], size = sizeof haystack, &index = 0)
{
for(new i; i < size; i++)
{
if(haystack[i] == needle)
{
index = i;
return true;
}
}
return false;
}
Re: Valid letters problem -
uZ1 - 11.10.2014
Wow it works! Thank you Vince.