strcmp
#1

Hello , I want to check if the player's name contains one "_" atleast or not, but it isn't working!

pawn Код:
new playernametest[MAX_PLAYER_NAME], numberof=0;
    GetPlayerName(playerid, playernametest, MAX_PLAYER_NAME);
    for (new j=0; j<= strlen(playernametest)-1;j++)
    {
        printf("%c", playernametest[j]);
        if(!strcmp("_", playernametest[j]))
        {
        numberof++;
        printf(" _ found at, %i", j);
        }
    }
    printf("numberof = %i", numberof);
results of printf tests :
Код:
[14:31:14] S
[14:31:14] a
[14:31:14] r
[14:31:14] r
[14:31:14] a
[14:31:14] _
[14:31:14] T
[14:31:14] e
[14:31:14] s
[14:31:14] t
[14:31:14] i
[14:31:14] n
[14:31:14] g
[14:31:14] _
[14:31:14] N
[14:31:14] a
[14:31:14] m
[14:31:14] e
[14:31:14] numberof = 0
it didn't print this
pawn Код:
if(!strcmp("_", playernametest[j]))
        {
        numberof++;
        printf(" _ found at, %i", j); //this
        }
Reply
#2

This is a character not a string.
Use
pawn Код:
if (playernametest[j] == '_') // ' not "
Reply
#3

I think strcmp is not correct for searching, look example
pawn Код:
new name[9] = "Chary";
new name2[9] = "Chary";
if(strcmp(name, name2))
they are compared true.
While:
pawn Код:
new name[9] = "Chary";
new name2[2] = "C";
for(new i=0; i < strlen(name); i++)
{
// strfinding
if(strfind('C', name[i], true) != -1) return true;
}
return 0;
strfind to find a part in a source (text)
strcmp to compare between two strings to check if they're the same,
or i'm wrong?
Reply
#4

Quote:
Originally Posted by Sawalha
Посмотреть сообщение
I think strcmp is not correct for searching, look example
pawn Код:
new name[9] = "Chary";
new name2[9] = "Chary";
if(strcmp(name, name2))
they are compared true.
While:
pawn Код:
new name[9] = "Chary";
new name2[2] = "C";
for(new i=0; i < sizeof(name); i++)
{
// strfinding
if(strfind('C', name[i], true) != -1) return true;
}
return 0;
strfind to find a part in a source (text)
strcmp to compare between two strings to check if they're the same,
or i'm wrong?
What he's doing is right. The only problem is that he's using strcmp to compare characters. I posted how to fix that.
Reply
#5

Have you not heard of Strfind ?

EDIT:
pawn Код:
new playernametest[MAX_PLAYER_NAME] = "omg_best_test_ever_1", numberof = 0, var;
    //GetPlayerName(playerid, playernametest, MAX_PLAYER_NAME);
    while((var = (strfind(playernametest, "_", false))) != -1)
    {
        printf(" _ found at, %i", var);
        strdel(playernametest, var, var + 1);
        strins(playernametest, " ", var); //You can use other functions such as 'strreplace' to achieve the same result
        numberof++;
    }
    printf("numberof = %i", numberof);
Returns:
Код:
 _ found at, 3
 _ found at, 8
 _ found at, 13
 _ found at, 18
numberof = 4
Reply
#6

Quote:
Originally Posted by Stinged
Посмотреть сообщение
This is a character not a string.
Use
pawn Код:
if (playernametest[j] == '_') // ' not "
thanks +rep
sorry I was dealing with characters in the same way as strings,
for the 2 other replies: thanks both of you and no, I've never heard about strfind, I'll learn about it now +rep for both
Reply
#7

Oh of course, it's just a character, my bad.

I still recommend using this:
pawn Код:
new playernametest[MAX_PLAYER_NAME], numberof = 0, var;
    GetPlayerName(playerid, playernametest, MAX_PLAYER_NAME);
    while((var = (strfind(playernametest, "_", false))) != -1)
    {
        printf(" _ found at, %i", var);
        playernametest[var] = ' ';
        numberof++;
    }
    printf("numberof = %i", numberof);
Reply
#8

Quote:
Originally Posted by Threshold
Посмотреть сообщение
Oh of course, it's just a character, my bad.

I still recommend using this:
pawn Код:
new playernametest[MAX_PLAYER_NAME], numberof = 0, var;
    GetPlayerName(playerid, playernametest, MAX_PLAYER_NAME);
    while((var = (strfind(playernametest, "_", false))) != -1)
    {
        printf(" _ found at, %i", var);
        playernametest[var] = ' ';
        numberof++;
    }
    printf("numberof = %i", numberof);
Alright thanks, +rep'ed u already
Reply
#9

someone answer my topic please!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)