Valid letters problem
#1

Код:
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.
Reply
#2

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
Reply
#3

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;
}
Reply
#4

Wow it works! Thank you Vince.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)