AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How to detect numbers and letters in an string? (https://forums.alliedmods.net/showthread.php?t=96119)

_lol_ 06-30-2009 20:44

How to detect numbers and letters in an string?
 
i think i have not to much to say, i want to "read" and string, and if it has a letter, block it, or if it has more than 3 numbers, block it.how to do it?

Alka 06-30-2009 20:55

Re: How to detect numbers and letters in an string?
 
PHP Code:

/* Returns true when value is digit. */
native isdigit(ch);

/* Returns true when value is letter. */
native isalpha(ch); 


_lol_ 06-30-2009 21:20

Re: How to detect numbers and letters in an string?
 
yeah..buy how should i use it?
look:

PHP Code:

public check(id)
{
    new 
say[300]
    
read_args(saycharsmax(say))
    
    
// Preparing the string
    
remove_quotes(say), trim(say)
    
    
// Only pressed enter?
    
if(equal(say""))
    {
        
zp_colored_print(id"^x04[ZP] ERROR !!!")
        return 
PLUGIN_HANDLED
    
}
    
// Two words?
    
if(contain(say" ") != -1)
    {
        
zp_colored_print(id"^x04[ZP]")
        return 
PLUGIN_HANDLED
    
}
    
// Very high number ??
    
if (str_to_num(say) > 255)
    {
        
zp_colored_print(id"^x04[ZP] ERROR !!!^x01 ")
        return 
PLUGIN_HANDLED
        
    
}
    
// Yeah, Passed !!
    
numbering(idsay)
    
    return 
PLUGIN_CONTINUE


i want to add to that check if the string has not more than 3 numbers, and if it has or not any letters. could you do it?:D

Bugsy 06-30-2009 23:54

Re: How to detect numbers and letters in an string?
 
PHP Code:

CountLetters( const szString[] )
{
    new 
iCount;
    
    for ( new 
strlenszString ) ; i++ )
        if ( 
isalphaszString] ) )
            
iCount++;
    
    return 
iCount;
}

CountNumbers( const szString[] )
{
    new 
iCount;
    
    for ( new 
strlenszString ) ; i++ )
        if ( 
isdigitszString] ) )
            
iCount++;
    
    return 
iCount;



Arkshine 07-01-2009 03:39

Re: How to detect numbers and letters in an string?
 
Don't use strlen() in a loop Bugsy :p.


Try this if String was not modified before:

Code:
CountLetters( const String[], const Len = sizeof( String ) ) {     new Count;         for ( new i = 0 ; i < Len ; i++ )     {         if ( isalpha( String[ i ] ) )  { Count++; }     }         return Count; } CountNumbers( const String[], const Len = sizeof( String ) ) {     new Count;         for ( new i = 0 ; i < Len; i++ )     {         if ( isdigit( String[ i ] ) )  { Count++; }     }         return Count; }

Otherwise use this :

Code:
CountLetters( const String[] ) {     new Count;     new Len = strlen( String );         for ( new i = 0 ; i < Len ; i++ )     {         if ( isalpha( String[ i ] ) )  { Count++; }     }         return Count; } CountNumbers( const String[] ) {     new Count;     new Len = strlen( String );         for ( new i = 0 ; i < Len; i++ )     {         if ( isdigit( String[ i ] ) )  { Count++; }     }         return Count; }

_lol_ 07-01-2009 10:40

Re: How to detect numbers and letters in an string?
 
yeah ! thanks !! and...is there a way to detect signs?(i mean !"·$%&.,/() etc.)

Bugsy 07-01-2009 10:41

Re: How to detect numbers and letters in an string?
 
Quote:

Originally Posted by arkshine (Post 861421)
Don't use strlen() in a loop Bugsy :p.

My mistake :avast:

You left out a closing brace for the for-loop in both CountNumbers functions

_lol_ 07-01-2009 10:56

Re: How to detect numbers and letters in an string?
 
PHP Code:

// Letters?
    
if (Countletters(saycharsmax(say) >= 1))
    {
        
zp_colored_print(id"^x04[Positive ZP6] ERROR !!!^x01 only numbers. Try it again")
        return 
PLUGIN_HANDLED
    


yeah, but if the string is "1dd" it pass, why?lol xd

TheRadiance 07-01-2009 11:11

Re: How to detect numbers and letters in an string?
 
Quote:

// Letters?
if (Countletters(say, charsmax(say) >= 1))
{
zp_colored_print(id, "^x04[Positive ZP6] ERROR !!!^x01 only numbers. Try it again")
return PLUGIN_HANDLED
}
It will count letters, not detect if string is containing only chars.

_lol_ 07-01-2009 11:25

Re: How to detect numbers and letters in an string?
 
so what sould i do?T_T


All times are GMT -4. The time now is 15:43.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.