Replace ' ' with '_'
#1

So i'm trying to make some sort of stock that replaces a space with an underscore, like the opposite of what you do if you have a roleplay server. Everything I've tried has failed, and everything i've found on ****** was failed me.
Reply
#2

pawn Код:
for(new i = 0, l = strlen(string); i < l; i++)
    {
        if(string[i] == ' ')
        {
            string[i] = '_';
        }
    }
?
Reply
#3

pawn Код:
stock AddUnderscore(string[])
{
    for(new i = 0, l = strlen(string); i < l; i++)
    {
        if(string[i] == ' ')
        {
            string[i] = '_';
        }
    }
    return string;
}

format(string, sizeof(string), "%s", AddUnderscore(PlayerData[playerid][Fullname]));
printf("%s", string);
SetPlayerName(playerid, string);
Print:
Код:
[00:42:41] T
Hint: PlayerData[playerid][Fullname] = Jack Leslie
Needs to return Jack_Leslie
Reply
#4

Just tested it and it works for me. It looks like you can't return the parameter "string".

Working function for your scenario:
pawn Код:
AddUnderscore(string[])
{
    new manipulated[MAX_PLAYER_NAME];
    for(new i = 0, l = strlen(string); i < l; i++)
    {
        manipulated[i] = string[i];
        if(string[i] == ' ')
        {
            manipulated[i] = '_';
        }
    }
    return manipulated;
}
Btw, why did you add "stock" to the function?
Reply
#5

Try this.

Код:
stock AddUnderscore(string[])
{
       str_replace(" ", "_", string);
       return string;
}
pawn Код:
stock str_replace(sSearch[], sReplace[], const sSubject[], &iCount = 0) //Credits to "Westie" for this function.
{
    new
        iLengthTarget = strlen(sSearch),
        iLengthReplace = strlen(sReplace),
        iLengthSource = strlen(sSubject),
        iItterations = (iLengthSource - iLengthTarget) + 1;

    new
        sTemp[128],
        sReturn[_strlib_med_string];

    strcat(sReturn, sSubject, _strlib_med_string);
    iCount = 0;

    for(new iIndex; iIndex < iItterations; ++iIndex)
    {
        strmid(sTemp, sReturn, iIndex, (iIndex + iLengthTarget), (iLengthTarget + 1));

        if(!strcmp(sTemp, sSearch, false))
        {
            strdel(sReturn, iIndex, (iIndex + iLengthTarget));
            strins(sReturn, sReplace, iIndex, iLengthReplace);

            iIndex += iLengthTarget;
            iCount++;
        }
    }

    return sReturn;
}
Reply
#6

Quote:
Originally Posted by Mellnik
Посмотреть сообщение
Just tested it and it works for me. It looks like you can't return the parameter "string".

Working function for your scenario:
pawn Код:
AddUnderscore(string[])
{
    new manipulated[MAX_PLAYER_NAME];
    for(new i = 0, l = strlen(string); i < l; i++)
    {
        manipulated[i] = string[i];
        if(string[i] == ' ')
        {
            manipulated[i] = '_';
        }
    }
    return manipulated;
}
Btw, why did you add "stock" to the function?
That worked a treat, thanks mate.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)