strfind
#1

Hello guys I'm trying to check if in a string there are only letters. I want to do it myself, so I tried 2 days and it still doesn't work. Can you please tell me what I'm doing wrong, I don't want anybody to give me the code, I just want to describe what piece of my code is wrong and what to change! Here is it:
PHP код:
bool:OnlyLetters(playerid, const text[])
{
    new 
chars 1;
    for(new 
'a'<= 'z'i++)
    {
        new 
string[256];
        
format(stringsizeof(string), "%c"i);
        for(new 
0<= strlen(text); l++)
        {
            if(
strfind(text[l], stringfalse)) chars++;
        }
        
format(stringsizeof(string), "%d, %d"charsstrlen(text));
        
SendClientMessage(playeridCOLOR_REDstring);
        if(
chars != strlen(text)) return false;
        else return 
true;
    }
    return 
false;

Input: "hello123" and the variable chars shows as 10, it has to be 5 and ignore the other 3 chars("123").
Reply
#2

What you doing here is
selects a to z characters one by one.On each step you are stroing this single character to a string.Then starts another iteration from beginning of the passed string text to it's end.Then you are passing from the "l"th index of text and characters from a to z (string)to strfind.So if "hello" is the text[] each time (in l loop) you are passing following to strfind
Код:
hello
ello
llo
lo
o
Also
strfind returns the number of characters before the sub string (the sub string's start position) or -1 if it's not found. (from wiki)

Therefore clearly the counting variable chars wont have number you are expecting

What you can possibly do is start from 0th index of the string to it's end and check if the character is an non alphabetic character if yes stop the program flow and return false and if iteration completes return true
PHP код:
= -
WHILE string[++i] do 
    IF 
NOT('a'<=string[i]<='z') AND NOT('A'<=string[i]<='Z'then
        
RETURN FALSE
    
ENDIF
ENDWHILE
RETURN 
TRUE 
Though,more easier method would be using regex
Reply
#3

Quote:
Originally Posted by SyS
Посмотреть сообщение
What you doing here is
selects a to z characters one by one.On each step you are stroing this single character to a string.Then starts another iteration from beginning of the passed string text to it's end.Then you are passing from the "l"th index of text and characters from a to z (string)to strfind.So if "hello" is the text[] each time (in l loop) you are passing following to strfind
Код:
hello
ello
llo
lo
o
Also
strfind returns the number of characters before the sub string (the sub string's start position) or -1 if it's not found. (from wiki)

Therefore clearly the counting variable chars wont have number you are expecting

What you can possibly do is start from 0th index of the string to it's end and check if the character is an non alphabetic character if yes stop the program flow and return false and if iteration completes return true
PHP код:
= -
WHILE string[++i] do 
    IF 
NOT('a'<=string[i]<='z') OR NOT('A'<=string[i]<='Z'then
        
RETURN FALSE
    
ENDIF
ENDWHILE
RETURN 
TRUE 
Though,more easier method would be using regex
I have just fixed my code, I did really stupid mistakes, so thanks for your help, but I did it myself. I'm still learning
Here is how I fixed it:
PHP код:
bool:OnlyLetters(const text[])
{
    new 
chars strlen(text)+1;
    for(new 
'a'<= 'z'i++)
    {
        new 
string[256];
          
format(stringsizeof(string), "%c"i);
        for(new 
0<= strlen(text); l++)
        {
            if(
strcmp(text[l], " "true) == 1)
            if(
strcmp(text[l], stringtrue) == 1){} else chars--;
        }
        if(
chars != strlen(text)) return false;
        else return 
true;
    }
    return 
false;

Reply
#4

Quote:
Originally Posted by Kraeror
Посмотреть сообщение
I have just fixed my code, I did really stupid mistakes, so thanks for your help, but I did it myself. I'm still learning
Here is how I fixed it:
PHP код:
bool:OnlyLetters(const text[])
{
    new 
chars strlen(text)+1;
    for(new 
'a'<= 'z'i++)
    {
        new 
string[256];
          
format(stringsizeof(string), "%c"i);
        for(new 
0<= strlen(text); l++)
        {
            if(
strcmp(text[l], " "true) == 1)
            if(
strcmp(text[l], stringtrue) == 1){} else chars--;
        }
        if(
chars != strlen(text)) return false;
        else return 
true;
    }
    return 
false;

That code won't work.You are over complicating your code.There's no need for that string variable itself and the outer loop iterates only once.A string is collection or array of characters.You can check each character one by one and check if it is not alphabet(as i mentioned in pseudo code in above post).Or just use regular expression "^[A-Za-z]+$"
Reply
#5

Quote:
Originally Posted by SyS
Посмотреть сообщение
That code won't work.You are over complicating your code.There's no need for that string variable itself and the outer loop iterates only once.A string is collection or array of characters.You can check each character one by one and check if it is not alphabet(as i mentioned in pseudo code in above post).Or just use regular expression "^[A-Za-z]+$"
I checked your code and:
Quote:

C:\Users\Dakzde\Desktop\Servers and Sites\Bulgarian Server\gamemodes\BG.pwn(51) : error 001: expected token: ";", but found "-identifier-"
C:\Users\Dakzde\Desktop\Servers and Sites\Bulgarian Server\gamemodes\BG.pwn(51) : error 017: undefined symbol "WHILE"
C:\Users\Dakzde\Desktop\Servers and Sites\Bulgarian Server\gamemodes\BG.pwn(51) : error 017: undefined symbol "string"
C:\Users\Dakzde\Desktop\Servers and Sites\Bulgarian Server\gamemodes\BG.pwn(51) : fatal error 107: too many error messages on one line

Error line:
PHP код:
WHILE string[++i] do 
Reply
#6

Quote:
Originally Posted by Kraeror
Посмотреть сообщение
I checked your code and:

Error line:
PHP код:
WHILE string[++i] do 
That was just a pseudo code to give you an idea about coding it
Reply
#7

Anybody else ?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)