PDA

View Full Version : strtolower?


Kemula
30/05/2016, 07:35 PM
Hello. Is there any include/plugin or function to use like strtolower? I know that tolower is for characters only (read from the SAMP's wiki).

Thank you :)

AndreT
30/05/2016, 07:39 PM
Try to implement the logic yourself. You have to create a function that takes all the characters in string A and applies the function tolower (https://wiki.sa-mp.com/wiki/Tolower) on them, then puts them in string B in the same order.

zSuYaNw
30/05/2016, 07:49 PM
#define strLower(%0) \
static i, e; i = 0, e = strlen(%0); for( ; i != e; ++i) %0[i] |= (1 << 5)// By zSuYaNw

how to use

new ASD[200];

ASD = "THE BOMB HAS BEEN PLANTED";

strLower(ASD);

printf(ASD);

Out: the bomb has been planted

Kemula
30/05/2016, 09:01 PM
Try to implement the logic yourself. You have to create a function that takes all the characters in string A and applies the function tolower (https://wiki.sa-mp.com/wiki/Tolower) on them, then puts them in string B in the same order.

Yeah! That was my plan B if there is no built-in function. Thank you :)


#define strLower(%0) \
static i, e; i = 0, e = strlen(%0); for( ; i != e; ++i) %0[i] |= (1 << 5)// By zSuYaNw

how to use

new ASD[200];

ASD = "THE BOMB HAS BEEN PLANTED";

strLower(ASD);

printf(ASD);

Out: the bomb has been planted

Thank you for that macro, can you explain it? There are things I've never seen before like |=

Vince
30/05/2016, 10:06 PM
Damn, just write a function. I don't understand this affection for large macros. They're hardly readable and they increase the size of the compiled code because it is copied to every location is is being used rather than being referenced from a single location.

To understand what is happening, refer to a character table, like this one: http://www.asciitable.com/
You will notice that capital letters and lowercase letters are exactly 32 characters apart from each other. Thus, to convert an uppercase letter to a lowercase one, one must only add 32. Conversely, to convert a lowercase letters to an uppercase one, subtract 32. This is what the (1 << 5) boils down to, only in less readable way.

The pipe symbol means bitwise OR, which combines all the set bits in one number with all set bits in another number to form a number that has all bits turned on that are in either number. But in this case a regular addition (+=) might as well have been used.

And then we see that this macro has a huge flaw: it doesn't check for any other characters or punctuation. So if any of those are present they will be garbled up.

SickAttack
31/05/2016, 01:29 AM
stock CapitalizeString(string[])
{
for(new i = 0, j = strlen(string); i < j; i ++)
{
string[i] = toupper(string[i]);
}
return 1;
}

stock UncapitalizeString(string[])
{
for(new i = 0, j = strlen(string); i < j; i ++)
{
string[i] = tolower(string[i]);
}
return 1;
}

SyS
31/05/2016, 02:51 AM
the difference of ascii value of lower and upper case is 32 where lower case value is greater so using this logic in a loop of an string array u can convert upper to lower and vice versa

SickAttack
31/05/2016, 03:07 AM
the difference of ascii value of lower and upper case is 32 where lower case value is greater so using this logic in a loop of an string array u can convert upper to lower and vice versa

Toupper & tolower exist. They avoid that issue.

justice96
31/05/2016, 03:10 AM
#define strToLower(%0) \
for(new i; %0[i] != EOS; ++i) \
%0[i] = ('A' <= %0[i] <= 'Z') ? (%0[i] += 'a' - 'A') : (%0[i])

Kemula
31/05/2016, 08:02 AM
Damn, just write a function. I don't understand this affection for large macros. They're hardly readable and they increase the size of the compiled code because it is copied to every location is is being used rather than being referenced from a single location.

To understand what is happening, refer to a character table, like this one: http://www.asciitable.com/
You will notice that capital letters and lowercase letters are exactly 32 characters apart from each other. Thus, to convert an uppercase letter to a lowercase one, one must only add 32. Conversely, to convert a lowercase letters to an uppercase one, subtract 32. This is what the (1 << 5) boils down to, only in less readable way.

The pipe symbol means bitwise OR, which combines all the set bits in one number with all set bits in another number to form a number that has all bits turned on that are in either number. But in this case a regular addition (+=) might as well have been used.

And then we see that this macro has a huge flaw: it doesn't check for any other characters or punctuation. So if any of those are present they will be garbled up.

Thanks! You've cleared some questions I had.

Thanks everybody :)