tolower output
#11

Quote:
Originally Posted by LetsOWN[PL]
View Post
Hi
pawn Code:
stock xToLower(string[])
{
new output[128];
if(strlen(string) > 128)
{
print("Input is too long! Max: 128 signs");
return 1;
}

for(new char = 0; char < strlen(string); char++)
{
format(output, 128, "%s%c", output, tolower(string[char]));
}
return output;
}
Maybe this?
Usage:
pawn Code:
xToLower(string[]);
Example:
pawn Code:
new String[12];
format(String, 12, "aCAUiFIUA");
printf("%s",xToLower(String));
Greetz,
LetsOWN
There are too many unnecessary checks and slow function usage in your code.
Shabi RoxX's direct replacement method should be the fastest and simplest I think. It has no limit on the input string size because it doesn't even need another string to store the output.
I also doubt "char" can be used as a variable/iterator as it's a reserved word. You can see the blue highlighting on it in your post.
Using strlen in the for loop check may also be slow for long strings (though the input string is short in this case). You should do something like
pawn Code:
for(new i = 0, len = strlen(string);i < len;i++)
Using format for simple string concatenation is also much slower than using strcat (I tested long time ago and found that it's about 4 to 5 times faster). I'll only use format when 3 to 4 strcats and strvals can't finish the job, or when I need special outputs (like hex). Using format on constant string is even slower than direct assignment. While you can simply do
pawn Code:
new String[12] = "aCAUiFIUA";
//or
new String[12];
String = "aCAUiFIUA";
, why do you need to use format?
Printing a single string can just use print(xToLower(String)).

Some may argue that we can't see the actual speed difference because the CPU is so fast, or they're just a little things that shouldn't be cared about too much, but at least it looks better and simpler, it's a good scripting practise.
Reply


Messages In This Thread
tolower output - by Dotayuri - 13.02.2013, 16:43
Re: tolower output - by Shabi RoxX - 13.02.2013, 16:48
Re: tolower output - by Dotayuri - 13.02.2013, 16:52
Re: tolower output - by Dotayuri - 13.02.2013, 16:57
Re: tolower output - by Shabi RoxX - 13.02.2013, 16:59
Re: tolower output - by Mmartin - 13.02.2013, 16:59
Re: tolower output - by S0n1COwnsYou - 13.02.2013, 17:00
Re: tolower output - by LetsOWN[PL] - 13.02.2013, 17:03
Re: tolower output - by Dotayuri - 13.02.2013, 17:08
Re: tolower output - by Jefff - 13.02.2013, 19:16
Re: tolower output - by leong124 - 14.02.2013, 19:03

Forum Jump:


Users browsing this thread: 1 Guest(s)